342. Power of Four
Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy
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; } }
326. Power of Three
Total Accepted: 37750 Total Submissions: 103178 Difficulty: Easy
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; } }
231. Power of Two
Total Accepted: 70238 Total Submissions: 192439 Difficulty: Easy
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; } }
时间: 2024-10-11 17:03:24