↓Skip to main content
  1. LeetCode/

LeetCode 66: Plus One

·2 mins· ·
LeetCode Easy Array Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-05-11

  • Submissions: Runtime Error ×3 → Wrong Answer → Accepted ×4 → Runtime Error ×2 → Accepted → Wrong Answer ×4 → Accepted ×3

Approach
#

Column addition (Math Template 6): from the last digit, a non-9 digit just increments and we return; a 9 becomes 0 and the carry moves left. If the loop finishes, every digit was 9, so prepend a 1.

My Attempts
#

The early versions all converted to an integer, added one and split it back:

  • 2022-05: ''.join(digits) on integers raised TypeError; after fixing that, str(...).split() splits on whitespace, so the whole number is one element: [9] returned [10] instead of [1, 0].
  • 2022-08 computed digits[i] * 10 ** k by hand: first as 10^k, but ^ is XOR in Python: [1] returned [1, 1]; then 10**len(digits)-1-i lacked parentheses and meant 10**len - 1 - i: [1] returned [1, 0].
  • In 2022-10 I switched to the digit-by-digit carry above.

Solution
#

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        for i in range(len(digits) - 1, -1, -1):
            if digits[i] < 9:
                digits[i] += 1
                return digits
            digits[i] = 0
        return [1] + digits

Complexity
#

Time O(n), space O(1) apart from the new array when every digit is 9.

Mistakes to Avoid
#

  • 10^k is XOR in Python; powers are 10**k, and 10**len - 1 - i needs parentheses.
  • Return a list of digits; str(...).split() produces a single element.

Interview Explanation
#

I carry from the last digit: a non-9 increments and returns, a 9 becomes 0 and the carry continues, and an all-9 array gets a leading 1. Time O(n) without converting to an integer.

Related

LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 35: Search Insert Position
·2 mins
LeetCode Easy Array Binary-Search
LeetCode 35 note: find the first position >= target (lower bound); a half-open [l, r) needs r = mid, not r = mid - 1
LeetCode 67: Add Binary
·2 mins
LeetCode Easy Math String Bit-Manipulation Simulation
LeetCode 67 note: add from the ends of both strings digit by digit, with carry sum // 2 and digit sum % 2
LeetCode 26: Remove Duplicates from Sorted Array
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 26 note: overwrite in place with a write pointer for the next slot, writing only values different from the last one written
LeetCode 14: Longest Common Prefix
·2 mins
LeetCode Easy Array String Trie
LeetCode 14 note: bound by the shortest string and check each position across all strings; the first mismatch gives the answer’s length
LeetCode 9: Palindrome Number
·2 mins
LeetCode Easy Math
LeetCode 9 note: negatives are never palindromes; without strings, reverse the second half of the digits and compare it with the first half