-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOfThree_326.java
More file actions
31 lines (28 loc) · 863 Bytes
/
Copy pathPowerOfThree_326.java
File metadata and controls
31 lines (28 loc) · 863 Bytes
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
package com.leetcode.array;
/**
* Created by charles on 2/20/17.
* 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 PowerOfThree_326 {
/**
* Time complexity : O(log_b(n))O(logb(n)). In our case that is O(log_3n)O(log3n). The number of divisions is given by that logarithm.
Space complexity : O(1)O(1). We are not using any additional memory.
*/
public boolean isPowerOfThree(int n) {
if (n < 1) {
return false;
}
while (n % 3 == 0) {
n /= 3;
}
return n == 1;
}
/**
* 3 ^ 19 is largest power of three less than Integer.MAX_VALUE (2147483647)
*/
public boolean isPowerOfThreeII(int n) {
return n > 0 && 1162261467 % n == 0;
}
}