标签:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Example:
Given num = 16, return true.
Given num = 5, return false.
public class Solution {
public boolean isPowerOfFour(int num) {
if(num==0)
return false;
while(num%4==0){
num = num>>2;
}
if(num==1)
return true;
return false;
}
}
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 Solution { public boolean isPowerOfThree(int num) { if(num==0) return false; while(num%3==0){ num = num/3; } if(num==1) return true; return false; } }
Given an integer, write a function to determine if it is a power of two.
public class Solution { public boolean isPowerOfTwo(int n) { if(n==0) return false; while(n%2==0){ n = n>>1; } if(n==1) return true; return false; } }
标签:
原文地址:http://www.cnblogs.com/coffy/p/5435142.html