forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode66_1.java
More file actions
64 lines (52 loc) · 1.37 KB
/
Leetcode66_1.java
File metadata and controls
64 lines (52 loc) · 1.37 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
package database;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Leetcode66_1 {
public static void main(String[] args) {
Leetcode66_1 ins = new Leetcode66_1();
int a[] = {1, 2, 3};
ins.plusOne(a);
}
public int[] plusOne(int[] digits) {
List<Integer> list1 = new ArrayList<>();
for (int i = digits.length - 1; i >= 0; i--) {
list1.add(digits[i]);
}
List<Integer> list2 = new ArrayList<Integer>(Collections.nCopies(list1.size(), 0));
list2.set(0, 1);
List<Integer> list3 = new ArrayList();
int carry = 0;
for (int i = 0; i < list1.size(); i++) {
int v = list1.get(i) + list2.get(i) + carry;
if (v >= 10) {
list3.add(0);
carry = 1;
} else {
list3.add(v);
carry = 0;
}
}
if (carry == 1) {
list3.add(1);
}
int r1[] = new int[list3.size()];
Collections.reverse(list3);
for (int i = 0; i < list3.size(); i++) {
r1[i] = list3.get(i);
}
return r1;
}
}
/**
* 执行用时 :
* 2 ms
* , 在所有 Java 提交中击败了
* 8.90%
* 的用户
* 内存消耗 :
* 35.7 MB
* , 在所有 Java 提交中击败了
* 36.83%
* 的用户
*/