sundeepblue
4/24/2014 - 7:03 PM

plus one. add one. Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that t

plus one. add one. Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list.

vector<int> plus_one(vector<int> &nums) {
    for (i=nums.size()-1; i>=0; i--) {
        if(nums[i] == 9) nums[i] = 0;
        else {
            nums[i] += 1;
            return nums;
        }
    }
    nums.insert(nums.begin(), 1);
    return nums;
}