Skip to content

Commit e83503f

Browse files
refactor 264
1 parent f38b821 commit e83503f

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,33 @@
1111
*/
1212
public class _264 {
1313

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+
}
3539
}
40+
return ugly[n - 1];
3641
}
37-
return ugly[n - 1];
3842
}
39-
4043
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,21 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by stevesun on 6/7/17.
11-
*/
129
public class _264Test {
13-
private static _264 test;
10+
private static _264.Solution1 solution1;
1411

1512
@BeforeClass
1613
public static void setup() {
17-
test = new _264();
14+
solution1 = new _264.Solution1();
1815
}
1916

2017
@Test
2118
public void test1() {
22-
assertEquals(12, test.nthUglyNumber(10));
19+
assertEquals(12, solution1.nthUglyNumber(10));
2320
}
2421

2522
@Test
2623
public void test2() {
27-
assertEquals(402653184, test.nthUglyNumber(1352));
24+
assertEquals(402653184, solution1.nthUglyNumber(1352));
2825
}
2926
}

0 commit comments

Comments
 (0)