First, the array --- plus one

Given a non-negative integer nonempty array of integers represented, on the basis of the number plus one.

Most significant digit stored in the first array, each element of the array stores a number only.

You may assume that in addition to the integer 0, the integer does not begin with a zero.

Example 1:

Input: [1,2,3]
Output: [2,4]
Explanation: numeral 123 denotes the input array.
Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: input array represents a number 4321.

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int>& digits) {
 4     int i=0;
 5     int size = digits.size();
 6     for(i=size-1;i>=0;i--){
 7         digits[i]++;
 8         digits[i] = digits[i]%10;
 9         if(digits[i] != 0) return digits;
10     }
11      // to the foregoing description for this step is recycled to the last loop did not return, are described carry occurred, 999,99 case 
12 is      IF (I == - . 1 ) {
 13 is          digits.insert (digits.begin () , . 1 );
 14      }
 15      return digits;
 16      }
 . 17 };

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/10990642.html