Solutions including digits = [9,9]


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

Log in to reply
 

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