# 加一

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

```输入: [1,2,3]

```输入: [4,3,2,1]

### java:

```class Solution {
public int[] plusOne(int[] digits) {
for( int i=digits.length;i>=0;i--){
if(digits[i]+1==10){
digits[i]=0;
}else {
digits[i]+=1;
break;
}
}
if(digits[0]==0){
int[] digits2=new int[digits.length+1];
digits2[0]=1;
return  digits2;
}else {
return digits;
}
}
}```

### python3:

```class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
"""
​
:type digits:int
:return: int
"""
num = 0
for i in range(len(digits)):
num = num*10 + digits[i]
return [int(i) for i in str(num+1)]
​```

python3则可以有很多实现方法，可以像以上java那种。

0 条评论

## 相关文章

10720

9930

### 《Java虚拟机原理图解》1.4 class文件中的字段表集合

13840

hasNext 可以看出，hasNext返回的是指针的nextIndex是否超过了链表长度。

8620

12150

16430

11320

9330

10930

### JAVA JVM JRE JDK

https://stackoverflow.com/questions/11547458/what-is-the-difference-between-jvm-...

10240