clueless coding // TODO: be smarter

LeetCode Plus One: CPP Solution



TL;DR Code Solution


vector<int> plusOne(vector<int>& digits) {
    int carry = 1;
    for (int i = digits.size() - 1; i >= 0; i--) {
        int current = digits[i];
        digits[i] = (current + carry) % 10;
        if (current + carry < 10) {
            carry = 0;
            break;
        }
    }
    if (carry) {
        digits.insert(digits.begin(), 1);
    }

    return digits;
}