-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
53 lines (47 loc) · 1.43 KB
/
Solution.java
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
package plusone;
import java.util.LinkedList;
/**
* Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
* <p>
* The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
* <p>
* You may assume the integer does not contain any leading zero, except the number 0 itself.
* <p>
* Example 1:
* <p>
* Input: [1,2,3]
* Output: [1,2,4]
* Explanation: The array represents the integer 123.
* Example 2:
* <p>
* Input: [4,3,2,1]
* Output: [4,3,2,2]
* Explanation: The array represents the integer 4321.
*/
class Solution {
public int[] plusOne(int[] digits) {
int carry = 0, sum = 0;
LinkedList<Integer> nums = new LinkedList<>();
for (int i = digits.length - 1; i >= 0; i--) {
if (i == digits.length - 1) {
sum = digits[i] + 1;
} else {
sum = carry + digits[i];
}
if (sum > 9) {
sum = 0;
carry = 1;
} else {
carry = 0;
}
nums.addFirst(sum);
}
if (carry != 0)
nums.addFirst(carry);
return nums.stream().mapToInt(i -> i).toArray();
}
}
/*
Runtime: 3 ms, faster than 11.38% of Java online submissions for Plus One.
Memory Usage: 38.4 MB, less than 5.64% of Java online submissions for Plus One.
*/