问题描述:

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

分析:非负整数,存储在数组中。the most significant digit 最高位存储在列表的第一个,例如:98, array[0] 存储9,array[1]存储8。

定义变量carry存储进位,遍历数组,每次修改digits[i] 与 carry

 public int[] plusOne(int[] digits) {
if(digits == null || digits.length == 0)
return null;
int len = digits.length;
int carry = 0; //进位,只借用一个变量,不需要数组
for(int i = len - 1; i >= 0; i--){
if(i == len - 1){
carry = (digits[i] + 1 ) / 10; //进位
digits[i] = (digits[i] + 1 ) % 10; //本位
} else {
int digit = (digits[i] + carry) % 10; //本位
carry = (digits[i] + carry) / 10; //进位
digits[i] = digit;
}
} if(carry == 0)
return digits;
else {
int[] new_digits = new int[len + 1];
for (int i = 1; i < new_digits.length; i++) {
new_digits[i] = digits[i - 1];
}
new_digits[0] = carry;
return new_digits;
}
}

Plus One leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  3. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  4. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  8. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  9. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  10. Reverse Words in a String leetcode java

    题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...

随机推荐

  1. Mysql优化知识点总结(转自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/MySQL.md 一.索引 B+ Tree 原理 MySQL 索引 索引优化 索引 ...

  2. 初识Java框架

    Spring boot>spring>spring mvc SSH:struts2+spring+hibernate SSM(SSH的改进): (过去)spring+struts2+MyB ...

  3. Vue.extend构造器和$mount实例构造组件后可以用$destroy()进行卸载,$forceUpdate()进行更新,$nextTick()数据修改

    html <div id="app"> </div> <p><button onclick="destroy()"&g ...

  4. .psl脚本介绍

    .ps1文件是PowerShell写好的脚本文件 可以在记事本中写一段PowerShell代码,然后将其保存为“xxx.ps1”,后面要使用它的时候,双击即可运行了.这有点像批处理的“.bat”文件, ...

  5. ZOJ 3632 ----dp+优先队列

    上个礼拜学长讲了优先队列的说.... emmmmmm.... 看着题解敲了一题...先m下. #include<cstring> #include<algorithm> #in ...

  6. docker 命令随笔

    如果是容器传输文件到本地的话,反过来就好了: docker cp  ID全称:容器文件路径   本地路径 2.进入docker 容器 docker exec -it fw-pay-trade-serv ...

  7. mybatis批量更新数据参考

    https://blog.csdn.net/xyjawq1/article/details/74129316

  8. Couldn't import dot_parser, loading of dot files will not be possible. 解决方法

    参考: pydot and graphviz error: Couldn't import dot_parser, loading of dot files will not be possible ...

  9. R语言可视化学习笔记之添加p-value和显著性标记--转载

    https://www.jianshu.com/p/b7274afff14f?from=timeline #先加载包 library(ggpubr) #加载数据集ToothGrowth data(&q ...

  10. hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥

    Senior PanⅡ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pr ...