LeetCode_Plus One

Given a number represented as an array of digits, plus one to the number.

  

class Solution {

public:

    vector<int> plusOne(vector<int> &digits) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        int n = digits.size();

        digits[n-1]++;

        for(n-- ;n>0 ;n--)

        {

          if(digits[n] >= 10)

          {

            digits[n]-= 10;

            digits[n-1]++;

          }else

             break;

        }

        

        if(digits[0] >= 10)

        {

            digits[0] -= 10;

            digits.insert(digits.begin(), 1);

        }

        

        return digits ;

    }

};

 

你可能感兴趣的:(LeetCode)