My simple c++ solution


  • 0
    C
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            int len = (int)digits.size();
            int i = 1;
    
            if(len == 0) {
                digits.push_back(1);
                return digits;
            }
    
            // find last digit that is not 9
            // if the last digits are all 9, set them to 0
            while(digits[len - i] == 9){
                digits[len - i] = 0;
                i++;
            }
    
            // if all digits is 9
            if(len - i < 0)
                digits.insert(digits.begin(), 1);
            else
                digits[len-i]++;
    
            return digits;
        }
    

    };


  • 0
    A
    This post is deleted!

Log in to reply
 

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