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

Commit0856174

Browse files
add a solution for 264
1 parent8c56452 commit0856174

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
packagecom.fishercoder.solutions;
22

3+
importjava.util.TreeSet;
4+
35
publicclass_264 {
46

57
publicstaticclassSolution1 {
@@ -31,4 +33,40 @@ public int nthUglyNumber(int n) {
3133
returnugly[n -1];
3234
}
3335
}
36+
37+
publicstaticclassSolution2 {
38+
/**
39+
* My completely original solution on 11/7/2021.
40+
* Although not super robust, as the input increases, I'll have to increase the times (variable n) on line 61 as some smaller numbers might appear later.
41+
*/
42+
publicintnthUglyNumber(intn) {
43+
TreeSet<Long>treeSet =newTreeSet<>();
44+
treeSet.add(1l);
45+
intcount =1;
46+
intpolled =0;
47+
int[]primes =newint[]{2,3,5};
48+
while (!treeSet.isEmpty()) {
49+
intsize =treeSet.size();
50+
for (inti =0;i <size;i++) {
51+
Longcurr =treeSet.pollFirst();
52+
polled++;
53+
if (polled ==n) {
54+
returncurr.intValue();
55+
}
56+
for (intprime :primes) {
57+
treeSet.add(prime *curr);
58+
count++;
59+
}
60+
}
61+
if (count >=n *3) {
62+
while (polled <n -1) {
63+
treeSet.pollFirst();
64+
polled++;
65+
}
66+
returntreeSet.pollFirst().intValue();
67+
}
68+
}
69+
return -1;
70+
}
71+
}
3472
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,59 @@
88

99
publicclass_264Test {
1010
privatestatic_264.Solution1solution1;
11+
privatestatic_264.Solution2solution2;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_264.Solution1();
16+
solution2 =new_264.Solution2();
1517
}
1618

1719
@Test
1820
publicvoidtest1() {
1921
assertEquals(12,solution1.nthUglyNumber(10));
22+
assertEquals(12,solution2.nthUglyNumber(10));
2023
}
2124

2225
@Test
2326
publicvoidtest2() {
2427
assertEquals(402653184,solution1.nthUglyNumber(1352));
28+
assertEquals(402653184,solution2.nthUglyNumber(1352));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(1,solution1.nthUglyNumber(1));
34+
assertEquals(1,solution2.nthUglyNumber(1));
35+
}
36+
37+
@Test
38+
publicvoidtest4() {
39+
assertEquals(2,solution1.nthUglyNumber(2));
40+
assertEquals(2,solution2.nthUglyNumber(2));
41+
}
42+
43+
@Test
44+
publicvoidtest5() {
45+
assertEquals(3,solution1.nthUglyNumber(3));
46+
assertEquals(3,solution2.nthUglyNumber(3));
47+
}
48+
49+
@Test
50+
publicvoidtest6() {
51+
assertEquals(5,solution1.nthUglyNumber(5));
52+
assertEquals(5,solution2.nthUglyNumber(5));
53+
}
54+
55+
@Test
56+
publicvoidtest7() {
57+
assertEquals(3888,solution1.nthUglyNumber(134));
58+
assertEquals(3888,solution2.nthUglyNumber(134));
59+
}
60+
61+
@Test
62+
publicvoidtest8() {
63+
assertEquals(536870912,solution1.nthUglyNumber(1407));
64+
assertEquals(536870912,solution2.nthUglyNumber(1407));
2565
}
2666
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp