C++ Easiest way to return the updated vector back (Explanation)

class Solution {
public:
vector plusOne(vector& digits) {

    int n = digits.size();
    int i=n-1;
    while(i>=0){
        if(digits[i]==9){
            digits[i] = 0;
        }
        else{
		    // To increment non 9 digits, here we must return
            digits[i]++; return digits;
        }
        i--;
    }
    
	//If it comes here that means there is only 9 digits present in vector
	//then we should put 0th index element as 1 and 0 at end
	//and in while loop we already replaced 9 with 0.
    digits[0] = 1;
    digits.push_back(0);
    return digits;
}

};

Comments (0)