[leetcode]Plus One

589 ワード

class Solution {

public:

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

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

        // DO NOT write int main() function

        if(digits.empty()) return vector<int>();

        

        int i = digits.size()-1;

        

        while(i >= 0 && digits[i] == 9){

            digits[i] = 0;

            i--;

        }

        

        if(i >= 0) digits[i]++;

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

        

        return digits;

        

    }

};