5 lines C++


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

Log in to reply
 

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