Title Description ： Given a non negative integer represented by a non empty array of integers , Add one to the number .

The highest digit is stored at the top of the array , Each element in the array stores only a single number .

You can assume that except for integers 0 outside , This integer does not start with zero .

Thinking of problem solving ：
To divide 3 In this paper, we discuss the situation ：
1. When input The last digit of is less than 9 The situation , Just add one at the end ;
2. A case involving a carry, for example [1,9]+, Let the last place be 0, The second highest position needs to enter one ;
3. When multiple carry is involved , such as [9,9]+, The size of the array will increase , There is one more place in the highest position .
class Solution { public: vector<int> plusOne(vector<int>& digits) { int len =
digits.size(); // Loop from last bit array for(int i = len - 1; i >= 0; --i){ // If a certain number of digits <9, In this position +1 that will do
if(digits[i] < 9){ ++digits[i]; return digits; } if(i == 0){
// When cycling to the highest position , Highest order assignment 1, And add in the last digit of the number 0（ For similar [9,9,9] Such an array ） digits[i] = 1;
//push_back Is to insert elements into an array digits.push_back(0); } else
// It hasn't been cycled to the highest bit and digits[i]=9 Time , This bit is assigned as 0 digits[i] = 0; } return digits; } };

Technology
Daily Recommendation