File tree Expand file tree Collapse file tree 2 files changed +30
-30
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +30
-30
lines changed Original file line number Diff line number Diff line change 11
11
*/
12
12
public class _264 {
13
13
14
- /**credit: https://discuss.leetcode.com/topic/21791/o-n-java-solution*/
15
- public int nthUglyNumber (int n ) {
16
- int [] ugly = new int [n ];
17
- ugly [0 ] = 1 ;
18
- int index2 = 0 ;
19
- int index3 = 0 ;
20
- int index5 = 0 ;
21
- int factor2 = 2 ;
22
- int factor3 = 3 ;
23
- int factor5 = 5 ;
24
- for (int i = 1 ; i < n ; i ++) {
25
- int min = Math .min (Math .min (factor2 , factor3 ), factor5 );
26
- ugly [i ] = min ;
27
- if (factor2 == min ) {
28
- factor2 = 2 * ugly [++index2 ];
29
- }
30
- if (factor3 == min ) {
31
- factor3 = 3 * ugly [++index3 ];
32
- }
33
- if (factor5 == min ) {
34
- factor5 = 5 * ugly [++index5 ];
14
+ public static class Solution1 {
15
+ /**
16
+ * credit: https://discuss.leetcode.com/topic/21791/o-n-java-solution
17
+ */
18
+ public int nthUglyNumber (int n ) {
19
+ int [] ugly = new int [n ];
20
+ ugly [0 ] = 1 ;
21
+ int index2 = 0 ;
22
+ int index3 = 0 ;
23
+ int index5 = 0 ;
24
+ int factor2 = 2 ;
25
+ int factor3 = 3 ;
26
+ int factor5 = 5 ;
27
+ for (int i = 1 ; i < n ; i ++) {
28
+ int min = Math .min (Math .min (factor2 , factor3 ), factor5 );
29
+ ugly [i ] = min ;
30
+ if (factor2 == min ) {
31
+ factor2 = 2 * ugly [++index2 ];
32
+ }
33
+ if (factor3 == min ) {
34
+ factor3 = 3 * ugly [++index3 ];
35
+ }
36
+ if (factor5 == min ) {
37
+ factor5 = 5 * ugly [++index5 ];
38
+ }
35
39
}
40
+ return ugly [n - 1 ];
36
41
}
37
- return ugly [n - 1 ];
38
42
}
39
-
40
43
}
Original file line number Diff line number Diff line change 6
6
7
7
import static org .junit .Assert .assertEquals ;
8
8
9
- /**
10
- * Created by stevesun on 6/7/17.
11
- */
12
9
public class _264Test {
13
- private static _264 test ;
10
+ private static _264 . Solution1 solution1 ;
14
11
15
12
@ BeforeClass
16
13
public static void setup () {
17
- test = new _264 ();
14
+ solution1 = new _264 . Solution1 ();
18
15
}
19
16
20
17
@ Test
21
18
public void test1 () {
22
- assertEquals (12 , test .nthUglyNumber (10 ));
19
+ assertEquals (12 , solution1 .nthUglyNumber (10 ));
23
20
}
24
21
25
22
@ Test
26
23
public void test2 () {
27
- assertEquals (402653184 , test .nthUglyNumber (1352 ));
24
+ assertEquals (402653184 , solution1 .nthUglyNumber (1352 ));
28
25
}
29
26
}
You can’t perform that action at this time.
0 commit comments