Here's my O(n) time solution


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

  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example


  • 0
    K

    How many ms did this solution take to pass the test case? I just got 4ms and wanted to compare.


Log in to reply
 

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