-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathAdd One To Number.java
More file actions
73 lines (61 loc) · 2.33 KB
/
Add One To Number.java
File metadata and controls
73 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Solution {
/**
* Adds one to a number represented as an ArrayList of integers.
*
* This method takes an ArrayList of integers representing the digits of a number
* and increments the number by one. It handles the carry-over logic when digits
* exceed 9, and manages the case where a new digit needs to be added (e.g., 999 + 1 = 1000).
* Leading zeros are removed from the result before returning.
*
* @param A an ArrayList of integers representing digits of a number (each element should be 0-9)
* @return an ArrayList of integers representing the digits of the incremented number,
* with leading zeros removed
*
* Example:
* Input: [1, 2, 3] (represents 123)
* Output: [1, 2, 4] (represents 124)
*
* Input: [9, 9, 9] (represents 999)
* Output: [1, 0, 0, 0] (represents 1000)
*/
public ArrayList<Integer> plusOne(ArrayList<Integer> A) {
// Step 1: Initialize carry to 1 (we're adding 1) and start from the last digit
int carry = 1;
int idx = A.size() - 1;
// Step 2: Process digits from right to left, handling the carry
while (idx >= 0) {
// Step 2a: Add current digit and carry
int temp = A.get(idx) + carry;
// Step 2b: Determine if there's a carry for the next digit (if sum > 9)
carry = temp > 9 ? 1 : 0;
// Step 2c: Keep only the last digit (0-9)
temp = temp > 9 ? temp % 10 : temp;
// Step 2d: Update the current digit with the new value
A.set(idx, temp);
// Step 2e: Stop if no carry, optimization to avoid unnecessary iterations
if (carry == 0) {
break;
}
// Step 2f: Move to the previous digit
idx--;
}
// Step 3: If there's still a carry after processing all digits,
// it means we need an extra digit at the front (e.g., 999 + 1 = 1000)
if (carry > 0) {
A.add(0, carry);
}
// Step 4: Remove leading zeros from the result
ArrayList<Integer> list = new ArrayList<>();
idx = 0;
// Step 4a: Skip all leading zeros
while (idx < A.size() && A.get(idx) == 0) {
idx++;
}
// Step 4b: Add all remaining digits to the result list
while (idx < A.size()) {
list.add(A.get(idx++));
}
// Step 5: Return the final result
return list;
}
}