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

Commit8973db1

Browse files
add a solution for 264
1 parent873165c commit8973db1

File tree

2 files changed

+20
-0
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+20
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,21 @@ public int nthUglyNumber(int n) {
6969
return -1;
7070
}
7171
}
72+
73+
publicstaticclassSolution3 {
74+
75+
publicintnthUglyNumber(intn) {
76+
TreeSet<Long>treeSet =newTreeSet<>();
77+
treeSet.add(1L);
78+
int[]arr =newint[]{2,3,5};
79+
longcurrentUgly =0;
80+
for (inti =0;i <n;i++) {
81+
currentUgly =treeSet.pollFirst();
82+
treeSet.add(currentUgly *arr[0]);
83+
treeSet.add(currentUgly *arr[1]);
84+
treeSet.add(currentUgly *arr[2]);
85+
}
86+
return (int)currentUgly;
87+
}
88+
}
7289
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
publicclass_264Test {
1010
private_264.Solution1solution1;
1111
private_264.Solution2solution2;
12+
private_264.Solution3solution3;
1213

1314
@BeforeEach
1415
publicvoidsetup() {
1516
solution1 =new_264.Solution1();
1617
solution2 =new_264.Solution2();
18+
solution3 =new_264.Solution3();
1719
}
1820

1921
@Test
2022
publicvoidtest1() {
2123
assertEquals(12,solution1.nthUglyNumber(10));
2224
assertEquals(12,solution2.nthUglyNumber(10));
25+
assertEquals(12,solution3.nthUglyNumber(10));
2326
}
2427

2528
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp