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
The title of the problem took a long time to understand. After understanding the good, feeling can also do.
It is important to note that when the number of results is increased, space needs to be increased for the vector. This requires the use of the insert in the vector. This is the first time to use, it feels good.
The code:
classSolution { Public: Vector<int> PlusOne (vector<int> &digits) { intValue=digits.size ()-1;//Set Length while(value>=0) { if(digits[value]<9) {Digits[value]+=1; Break; } Else{Digits[value]=0; Value--; }} Value++; if(value==0&&digits[value]==0) {Digits.insert (Digits.begin (),1);//This is a very important sentence . } returndigits; }};Tell me about the use of insert in a vector object: vector container using the Insert () method, you can insert a new element in the vector object before any position, the book says, the Insert () method requires the location of the insertion, is the position of the element iterator, Rather than the subscript of the element. A new element is added to the Digits.begin () in the code above.
Happyleetcode44:plus One