5-line C++ solution


  • 0
    E
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int c=1, s=0;
            for (int i=digits.size()-1; i>=0; i--) 
                digits[i] = ((s = digits[i]+c) <= 9) ? (c=0, s) : (c=1, 0);
            if (c > 0) digits.insert(digits.begin(), c);
            return digits;
        }
    };
    

Log in to reply
 

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