c++ simple solution (O(n))


  • 0
    vector<int> plusOne(vector<int>& digits) {
        int cur = 1;
        for (int i = digits.size() - 1; i >= 0; --i) {
            digits[i] += cur;
            cur = digits[i] / 10;
            digits[i] %= 10;
        }
        if (cur) digits.insert(digits.begin(), cur);
        return digits;
     }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.