forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_326.java
46 lines (41 loc) · 1.48 KB
/
_326.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.fishercoder.solutions;
/**
* 326. Power of Three
*
* Given an integer, write a function to determine if it is a power of three.
*
* Follow up:
* Could you do it without using any loop / recursion?
*/
public class _326 {
public static class Solution1 {
//regular method that has a loop
public boolean isPowerOfThree(int n) {
if (n < 3 && n != 1) {
return false;
}
while (n != 1) {
if (n % 3 != 0) {
return false;
}
n /= 3;
}
return true;
}
}
public static class Solution2 {
//find the max possible integer that is a power of 3, then do modulor with this number
public boolean isPowerOfThree(int n) {
return (n > 0 && 1162261467 % n == 0);
}
}
public static class Solution3 {
//Editorial solution: it's pretty elegant to use base conversion which can be easily extended to any radix k
//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
//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...
//some Java built-in function could help us along the way:
public boolean isPowerOfThree(int n) {
return Integer.toString(n, 3).matches("^10*$");
}
}
}