66. Plus One (Leetcode)

PHOTO EMBED

Mon Oct 04 2021 09:30:38 GMT+0000 (Coordinated Universal Time)

Saved by @Sakshamkashyap7 #c++

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();
        for(int i = n-1; i >=0 ; --i)   {
            if((digits[i] + 1) > 9) {
                digits[i] = 0;
            }
            else    {
                digits[i] += 1;
                return digits;
            }
        }
        digits.push_back(0);
        digits[0] = 1;
        return digits;
    }
};
content_copyCOPY

Adding 1 to last element in array Input: digits = [1,2,3] Output: [1,2,4] Input: digits = [9] Output: [1,0]

https://leetcode.com/problems/plus-one/