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

Commit 8973db1

Browse files
add a solution for 264
1 parent 873165c commit 8973db1

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+
public static class Solution3 {
74+
75+
public int nthUglyNumber(int n) {
76+
TreeSet<Long> treeSet = new TreeSet<>();
77+
treeSet.add(1L);
78+
int[] arr = new int[]{2, 3, 5};
79+
long currentUgly = 0;
80+
for (int i = 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
public class _264Test {
1010
private _264.Solution1 solution1;
1111
private _264.Solution2 solution2;
12+
private _264.Solution3 solution3;
1213

1314
@BeforeEach
1415
public void setup() {
1516
solution1 = new _264.Solution1();
1617
solution2 = new _264.Solution2();
18+
solution3 = new _264.Solution3();
1719
}
1820

1921
@Test
2022
public void test1() {
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)

AltStyle によって変換されたページ (->オリジナル) /