File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand Expand file tree Collapse file tree 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) {
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}
Original file line number Diff line number Diff line change 99public 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
You can’t perform that action at this time.
0 commit comments