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

Commit4be926f

Browse files
update a solution for 970
1 parent19e7a1b commit4be926f

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

‎src/main/java/com/fishercoder/solutions/_970.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@
88

99
publicclass_970 {
1010
publicstaticclassSolution1 {
11-
/**
12-
* This approach results in Time Limit Exceeded since it's apparently doing
13-
* redundant checks.
14-
*/
1511
publicList<Integer>powerfulIntegers(intx,inty,intbound) {
16-
Set<Integer>result =newHashSet<>();
17-
intsmall =x;
18-
intbig =y;
19-
if (x >y) {
20-
small =y;
21-
big =x;
22-
}
23-
intmaxPower =bound /small;
24-
for (inti =0;i <=maxPower +1;i++) {
25-
for (intj =0;j <=maxPower +1;j++) {
26-
intsum = (int) (Math.pow(small,i) +Math.pow(big,j));
27-
if (sum <=bound) {
28-
result.add(sum);
12+
intpowerInteger;
13+
Set<Integer>set =newHashSet<>();
14+
for (inti =0;i <=bound;i++) {
15+
for (intj =0;j <=bound;j++) {
16+
powerInteger = (int) (Math.pow(x,i) +Math.pow(y,j));
17+
if (powerInteger <=bound) {
18+
set.add(powerInteger);
19+
}else {
20+
break;
21+
}
22+
if (y ==1) {
23+
break;
2924
}
3025
}
26+
if (x ==1) {
27+
break;
28+
}
3129
}
32-
List<Integer>list =newArrayList<>(result);
33-
Collections.sort(list);
34-
returnlist;
30+
returnnewArrayList<>(set);
3531
}
3632
}
3733

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,60 @@
11
packagecom.fishercoder;
22

3+
importcom.fishercoder.common.utils.CommonUtils;
34
importcom.fishercoder.solutions._970;
5+
46
importjava.util.Arrays;
57
importjava.util.Collections;
68
importjava.util.List;
9+
710
importorg.junit.BeforeClass;
811
importorg.junit.Test;
912

1013
importstaticorg.junit.Assert.assertEquals;
1114

1215
publicclass_970Test {
13-
privatestatic_970.Solution1solution1;
14-
privatestatic_970.Solution2solution2;
15-
16-
@BeforeClass
17-
publicstaticvoidsetup() {
18-
solution1 =new_970.Solution1();
19-
solution2 =new_970.Solution2();
20-
}
21-
22-
@Test
23-
publicvoidtest1() {
24-
assertEquals(Arrays.asList(2,3,4,5,7,9,10),solution1.powerfulIntegers(2,3,10));
25-
assertEquals(Arrays.asList(2,3,4,5,7,9,10),solution2.powerfulIntegers(2,3,10));
26-
}
27-
28-
@Test
29-
publicvoidtest2() {
30-
assertEquals(Arrays.asList(2,4,6,8,10,14),solution1.powerfulIntegers(3,5,15));
31-
assertEquals(Arrays.asList(2,4,6,8,10,14),solution2.powerfulIntegers(3,5,15));
32-
}
33-
34-
@Test
35-
publicvoidtest3() {
36-
assertEquals(Arrays.asList(2,3,5,7,8,9,10),solution1.powerfulIntegers(2,6,12));
37-
assertEquals(Arrays.asList(2,3,5,7,8,9,10),solution2.powerfulIntegers(2,6,12));
38-
}
39-
40-
@Test
41-
publicvoidtest4() {
42-
assertEquals(Arrays.asList(2,3,5,9,10,11),solution1.powerfulIntegers(2,9,12));
43-
assertEquals(Arrays.asList(2,3,5,9,10,11),solution2.powerfulIntegers(2,9,12));
44-
}
45-
46-
@Test
47-
publicvoidtest5() {
48-
assertEquals(Arrays.asList(2,91,180,8101,8190,16200,729001,729090,
49-
737100),solution1.powerfulIntegers(90,90,1000000));
50-
List<Integer>actual =solution2.powerfulIntegers(90,90,1000000);
51-
Collections.sort(actual);
52-
assertEquals(Arrays.asList(2,91,180,8101,8190,16200,729001,729090,
53-
737100),actual);
54-
}
16+
privatestatic_970.Solution1solution1;
17+
privatestatic_970.Solution2solution2;
18+
19+
@BeforeClass
20+
publicstaticvoidsetup() {
21+
solution1 =new_970.Solution1();
22+
solution2 =new_970.Solution2();
23+
}
24+
25+
@Test
26+
publicvoidtest1() {
27+
assertEquals(Arrays.asList(2,3,4,5,7,9,10),solution1.powerfulIntegers(2,3,10));
28+
assertEquals(Arrays.asList(2,3,4,5,7,9,10),solution2.powerfulIntegers(2,3,10));
29+
}
30+
31+
@Test
32+
publicvoidtest2() {
33+
assertEquals(Arrays.asList(2,4,6,8,10,14),solution1.powerfulIntegers(3,5,15));
34+
assertEquals(Arrays.asList(2,4,6,8,10,14),solution2.powerfulIntegers(3,5,15));
35+
}
36+
37+
@Test
38+
publicvoidtest3() {
39+
assertEquals(Arrays.asList(2,3,5,7,8,9,10),solution1.powerfulIntegers(2,6,12));
40+
assertEquals(Arrays.asList(2,3,5,7,8,9,10),solution2.powerfulIntegers(2,6,12));
41+
}
42+
43+
@Test
44+
publicvoidtest4() {
45+
assertEquals(Arrays.asList(2,3,5,9,10,11),solution1.powerfulIntegers(2,9,12));
46+
assertEquals(Arrays.asList(2,3,5,9,10,11),solution2.powerfulIntegers(2,9,12));
47+
}
48+
49+
@Test
50+
publicvoidtest5() {
51+
CommonUtils.printList(solution1.powerfulIntegers(90,90,1000000));
52+
CommonUtils.printList(solution2.powerfulIntegers(90,90,1000000));
53+
}
54+
55+
@Test
56+
publicvoidtest6() {
57+
assertEquals(Arrays.asList(2),solution1.powerfulIntegers(1,1,40000));
58+
assertEquals(Arrays.asList(2),solution2.powerfulIntegers(1,1,40000));
59+
}
5560
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp