Leetcode 66 Solution

This article provides solution to leetcode question 66 (plus-one)

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

Solution

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int carry = 1;

        for (auto it = digits.rbegin(); it != digits.rend(); it++)
        {
            int val = carry + *it;

            if (val < 10)
            {
                *it = val;
                carry = 0;
            }
            else
            {
                *it = 0;
                carry = 1;
            }
        }

        if (carry == 1)
            digits.insert(digits.begin(), 1);

        return digits;
    }
};