File tree 2 files changed +20
-0
lines changed
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand
2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -69,4 +69,21 @@ public int nthUglyNumber(int n) {
69
69
return -1 ;
70
70
}
71
71
}
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
+ }
72
89
}
Original file line number Diff line number Diff line change 9
9
public class _264Test {
10
10
private _264 .Solution1 solution1 ;
11
11
private _264 .Solution2 solution2 ;
12
+ private _264 .Solution3 solution3 ;
12
13
13
14
@ BeforeEach
14
15
public void setup () {
15
16
solution1 = new _264 .Solution1 ();
16
17
solution2 = new _264 .Solution2 ();
18
+ solution3 = new _264 .Solution3 ();
17
19
}
18
20
19
21
@ Test
20
22
public void test1 () {
21
23
assertEquals (12 , solution1 .nthUglyNumber (10 ));
22
24
assertEquals (12 , solution2 .nthUglyNumber (10 ));
25
+ assertEquals (12 , solution3 .nthUglyNumber (10 ));
23
26
}
24
27
25
28
@ Test
You can’t perform that action at this time.
0 commit comments