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

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit