Given a non-negative number represented as an array of digits, plus one to the number.
The digits is stored such, the most significant digit was at the head of the list.
Public classSolution { Public int[] PlusOne (int[] digits) { //key point, you need to set a carry variable carry//carry the initial value is 1, the Representative plus 1, here is very flattering, if not set, you need to take the last one out to discuss//There is one more point to note: If the highest bit has carry, the resize array is required intlen=digits.length; //if (len<1) return digits; intCarry=1; for(inti=len-1;i>=0;i--){ inttemp=digits[i]+carry; Digits[i]=temp%10; Carry=temp/10; } if(carry>0){ int[] newdigits=New int[Len+1]; for(inti=1;i<len+1;i++) {Newdigits[i]=digits[i-1]; } newdigits[0]=carry; returnnewdigits; }Else returndigits; }}
[Leedcode 66] Plus One