Problem:
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.
Hide TagsArray MathTest instructions, a non-negative integer is saved in the form of an array, each bit 0~9, the number +1, returns its array form, requiring the highest number of digits in the array first
Thinking:
(1) First to read test instructions, this problem is not difficult.
(2) Attention to detail:
1.
while (a>0 &&--i>=0) { int tmp=digits[i]; digits[i]= (Digits[i]+a)%10; A= (Tmp+a)/10; }
Application of temporary variables
2, the last one should not carry, to consider the
Code
Class Solution {public: vector<int> plusone (vector<int> &digits) { int a=0; if (Digits.size () ==0) { digits.push_back (1); return digits; } int I=digits.size ()-1; A= (digits[i]+1)/10; digits[i]= (digits[i]+1)%10; while (a>0 &&--i>=0) { int tmp=digits[i]; digits[i]= (Digits[i]+a)%10; A= (Tmp+a)/10; } if (a>0) Digits.insert (Digits.begin (), a); return digits;} ;
Leetcode | | 66, Plus One