# 加一

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.

### 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;         }     } }

### 思路：

​ 指针从最后往前移动，若值为10逐个加一，并赋值0。不等于10则退出循环。首位如果为是0则证明需要进一。这里新建一个长度比原数组大一。只需首位赋值1即可。

### 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那种。