My 4ms C++ solution


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

Log in to reply
 

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