Recursive fun :) C++


  • 0
    P
    class Solution {
    public:
        int n = -2;
        vector<int> plusOne(vector<int>& digits) {
            if(n == -2) {
                n = digits.size() - 1;
                digits[n] += 1;
            }
            if(n == -1){
                if(*digits.begin() > 9){
                    digits.insert(digits.begin(), *digits.begin()/10);
                    *(digits.begin() + 1) %= 10;
                }
                return digits;
            }
            if(digits[n] < 10) return digits;
            if(digits[n] > 9 && n != 0){
                digits[n - 1] += digits[n] / 10;
                digits[n] %= 10;
            }
            n--;
            return plusOne(digits);
        }
    };

Log in to reply
 

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