Skip to content

Commit ad61a2a

Browse files
[N-0] refactor 326
1 parent 85bd4e5 commit ad61a2a

File tree

2 files changed

+60
-40
lines changed

2 files changed

+60
-40
lines changed
Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
11
package com.fishercoder.solutions;
22

3-
/**326. Power of Three
3+
/**
4+
* 326. Power of Three
45
*
5-
Given an integer, write a function to determine if it is a power of three.
6-
7-
Follow up:
8-
Could you do it without using any loop / recursion?
6+
* Given an integer, write a function to determine if it is a power of three.
7+
*
8+
* Follow up:
9+
* Could you do it without using any loop / recursion?
910
*/
1011

1112
public class _326 {
12-
//then I turned to the Editorial solution, it's pretty elegant to use base conversion which can be easily extended to any radix k
13-
//Idea: for a number in base 10, if it's power of 10, then it must be in this format: 10, 100, 1000... with a leading one and all trailing zeros
14-
//similarly, if a number is power of 3, then in its base 3 format, it must be in this format as well: 10, 100, 1000, 1000...
15-
//some Java built-in function could help us along the way:
16-
public boolean isPowerOfThree_base_conversion(int n) {
17-
return Integer.toString(n, n).matches("^10*$");
18-
}
19-
20-
//it turns out they're using a trick to solve this question without using a loop: find the max possible integer that is a power of 3, then do modulor with this number
21-
public boolean isPowerOfThree_without_loop(int n) {
22-
return (n > 0 && 1162261467 % n == 0);
23-
}
24-
25-
//I'm not able to think of a method that has no loop to do it, use regular method to solve it first
26-
public boolean isPowerOfThree(int n) {
27-
if (n < 3 && n != 1) {
28-
return false;
29-
}
30-
while (n != 1) {
31-
if (n % 3 != 0) {
13+
public static class Solution1 {
14+
//regular method that has a loop
15+
public boolean isPowerOfThree(int n) {
16+
if (n < 3 && n != 1) {
3217
return false;
3318
}
34-
n /= 3;
19+
while (n != 1) {
20+
if (n % 3 != 0) {
21+
return false;
22+
}
23+
n /= 3;
24+
}
25+
return true;
3526
}
36-
return true;
3727
}
3828

39-
public static void main(String... strings) {
40-
_326 test = new _326();
41-
System.out.println(test.isPowerOfThree(12));
29+
public static class Solution2 {
30+
//find the max possible integer that is a power of 3, then do modulor with this number
31+
public boolean isPowerOfThree(int n) {
32+
return (n > 0 && 1162261467 % n == 0);
33+
}
34+
}
4235

43-
//find the max integer that is power of 3
44-
int maxPowerOf3OneStepFurther = 3;
45-
int maxPowerOf3 = 0;
46-
while (maxPowerOf3OneStepFurther >= 0) {
47-
maxPowerOf3OneStepFurther = (int) maxPowerOf3OneStepFurther * 3;
48-
if (maxPowerOf3OneStepFurther > 0) {
49-
maxPowerOf3 = maxPowerOf3OneStepFurther;
50-
}
51-
System.out.println("maxPowerOf3 is: " + maxPowerOf3);
36+
public static class Solution3 {
37+
//Editorial solution: it's pretty elegant to use base conversion which can be easily extended to any radix k
38+
//Idea: for a number in base 10, if it's power of 10, then it must be in this format: 10, 100, 1000... with a leading one and all trailing zeros
39+
//similarly, if a number is power of 3, then in its base 3 format, it must be in this format as well: 10, 100, 1000, 1000...
40+
//some Java built-in function could help us along the way:
41+
public boolean isPowerOfThree(int n) {
42+
return Integer.toString(n, 3).matches("^10*$");
5243
}
5344
}
54-
}
45+
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._326;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _326Test {
10+
private static _326.Solution1 solution1;
11+
private static _326.Solution2 solution2;
12+
private static _326.Solution3 solution3;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _326.Solution1();
17+
solution2 = new _326.Solution2();
18+
solution3 = new _326.Solution3();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
assertEquals(false, solution1.isPowerOfThree(12));
24+
assertEquals(false, solution2.isPowerOfThree(12));
25+
assertEquals(false, solution3.isPowerOfThree(12));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)