Sharing my 4ms C++ solution


  • 0
    T
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int n = digits.size();
            int addOne = 1;
            int i;
            vector<int> result(n);
            
            for(i=n-1; i>=0; i--)
            {
                result[i] = addOne + digits[i];
                if(result[i] == 10)
                {
                    result[i] = 0;
                    addOne = 1;
                }
                else
                    addOne = 0;
            }
            
            if(addOne == 1)
                result.insert(result.begin(), 1);
                
            return result;
        }
    };

Log in to reply
 

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