Simplest C++ Solution


  • 0
    M
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int pos = digits.size() - 1;
            while (pos >= 0 && digits[pos] == 9) {
                digits[pos] = 0;
                --pos;
            }
            if (pos >= 0) ++digits[pos];
            else digits.insert(digits.begin(), 1);
            return digits;
        }
    };
    

Log in to reply
 

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