[leetcode刷题系列]Plus One

- - 模拟题


class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(digits.size() <= 0)
            return digits;
        reverse(digits.begin(), digits.end());
        int carry = 1;
        for(int i = 0; i < digits.size(); ++ i){
            digits[i] += carry;
            if(digits[i] >= 10){
                carry = digits[i] / 10;
                digits[i] %= 10;
            }else{
                carry = 0;
                break;
            }
        }
        if(carry)
            digits.push_back(carry);
        reverse(digits.begin(), digits.end());
        return digits;
    }
};


你可能感兴趣的:([leetcode刷题系列]Plus One)