-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1431- 拥有最多糖果的孩子.java
59 lines (36 loc) · 1.06 KB
/
1431- 拥有最多糖果的孩子.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
54
55
56
57
//方法一
import java.util.ArrayList;
import java.util.List;
class Solution {
public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max = 0;
for (int i : candies ) {
max = Math.max(max, i);
}
List<Boolean> re = new ArrayList<>();
for (int i = 0; i < candies.length; i++) {
re.add(candies[i] + extraCandies >= max);
}
return re;
}
}
//方法二
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int[] ints = Arrays.copyOf(candies, candies.length);
Arrays.sort(candies);
int max = candies[candies.length - 1];
List<Boolean> re = new ArrayList<>();
for (int i = 0; i < candies.length; i++) {
if (ints[i] + extraCandies >= max) {
re.add(true);
} else {
re.add(false);
}
}
return re;
}
}