LeetCode—66

Approach

# 模擬加法
Step-by-step addition simulation.

Time Complexity

# n = digits
O(n)

Space Complexity

# n = digits
O(n)

Code

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int l = digits.size(), carry = 0;

        digits[l - 1]++;
        for (int i = l - 1 ; i >= 0 ; i--) {
            digits[i] = digits[i] + carry;
            if (digits[i] >= 10) {
                digits[i] -= 10;
                carry = 1;
            }
            else {
                carry = 0;
            }
        }

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

        return digits;
    }
};