EASY NC#140 Math & Geometry
66. Plus One
๐ Problem
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's, except for the number 0 itself. Increment the large integer by one and return the resulting array of digits.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
๐ก Approach
- โ Process digits from right to left
- โ Add 1 and propagate carry if digit is 9
- โ If all digits are 9, need to add a leading 1
- โ Time: O(n), Space: O(1) or O(n) if new array needed
๐ ๏ธ Hints & Pitfalls
Hints
- โขProcess digits from right to left
- โขAdd 1 and propagate carry if digit is 9
- โขIf all digits are 9, need to add a leading 1
Common Pitfalls
- โขTime: O(n), Space: O(1) or O(n) if new array needed
๐งช Test Cases
Hidden tests on submit: 2
Test Case 1
Not run Input:
plusOne([1,2,3]); Expected:
[1, 2, 4] Test Case 2
Not run Input:
plusOne([4,3,2,1]); Expected:
[4, 3, 2, 2] Test Case 3
Not run Input:
plusOne([9]); Expected:
[1, 0] ๐ Code Editor
๐ค Output