Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd119cdf

Browse files
refactor 575
1 parent895ca1d commitd119cdf

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
packagecom.fishercoder.solutions;
22

3-
importjava.util.Arrays;
3+
importjava.util.HashSet;
4+
importjava.util.Set;
45

56
publicclass_575 {
67
publicstaticclassSolution1 {
7-
publicintdistributeCandies(int[]candies) {
8-
Arrays.sort(candies);
9-
intsisCount =0;
10-
for (inti =0;i <candies.length;i++) {
11-
intval =candies[i];
12-
sisCount++;
13-
if (sisCount >=candies.length /2) {
14-
returncandies.length /2;
15-
}
16-
while (i <candies.length &&candies[i] ==val) {
17-
i++;
18-
}
19-
i--;
8+
publicintdistributeCandies(int[]candyType) {
9+
Set<Integer>map =newHashSet<>();
10+
for (intcandy :candyType) {
11+
map.add(candy);
2012
}
21-
returnsisCount;
13+
returnMath.min(map.size(),candyType.length /2);
2214
}
2315
}
2416
}

‎src/test/java/com/fishercoder/_575Test.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
importstaticorg.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/6/17.
11-
*/
129
publicclass_575Test {
1310
privatestatic_575.Solution1solution1;
1411
privatestaticintexpected;
1512
privatestaticintactual;
16-
privatestaticint[]candies;
13+
privatestaticint[]candyType;
1714

1815
@BeforeClass
1916
publicstaticvoidsetup() {
@@ -22,25 +19,25 @@ public static void setup() {
2219

2320
@Test
2421
publicvoidtest1() {
25-
candies =newint[]{1,1,2,3};
22+
candyType =newint[]{1,1,2,3};
2623
expected =2;
27-
actual =solution1.distributeCandies(candies);
24+
actual =solution1.distributeCandies(candyType);
2825
assertEquals(expected,actual);
2926
}
3027

3128
@Test
3229
publicvoidtest2() {
33-
candies =newint[]{1,1,2,2,3,3};
30+
candyType =newint[]{1,1,2,2,3,3};
3431
expected =3;
35-
actual =solution1.distributeCandies(candies);
32+
actual =solution1.distributeCandies(candyType);
3633
assertEquals(expected,actual);
3734
}
3835

3936
@Test
4037
publicvoidtest3() {
41-
candies =newint[]{1000,1,1,1};
38+
candyType =newint[]{1000,1,1,1};
4239
expected =2;
43-
actual =solution1.distributeCandies(candies);
40+
actual =solution1.distributeCandies(candyType);
4441
assertEquals(expected,actual);
4542
}
4643
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp