-
Notifications
You must be signed in to change notification settings - Fork 0
231. Power of Two
Jacky Zhang edited this page Aug 11, 2016
·
1 revision
Given an integer, write a function to determine if it is a power of two.
##Approach 1
public class Solution {
public boolean isPowerOfTwo(int n) {
return n > 0 && Math.pow(2,30) % n == 0;
}
}##Approach 2 注意到power of 2有以下特征:8(1000),8-1(0111),8 & (8-1) == 0000。
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n < 1) return false;
return ((n-1) & n) == 0;
}
}