Neat C++ solution O(n)


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

Log in to reply
 

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