C++ Recursive Solution


  • 0
    I
    void plus_one_aux(std::vector<int>& digits, int pos) {
        if (pos < 0) {
            digits.insert(digits.begin(), 1);
        } else {
            if (digits[pos] == 9) {
                digits[pos] = 0;
                plus_one_aux(digits, pos-1);
            } else {
                digits[pos] += 1;
            }
        }
    }
    
    class Solution {
    public:
        vector<int> plusOne(vector<int>& digits) {
            plus_one_aux(digits, digits.size()-1);
            return digits;
        }
    };
    

Log in to reply
 

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