Power of Four(leetcode342)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.git

Example 1:github

Input: 16
Output: true

Example 2:oop

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?code

public static boolean isPowerOfFour(int num) {

    return num > 0 && (num&(num-1)) == 0 && (num & 0x55555555) != 0;

}

 

//前面是是不是2的冪  後面是能被3整除 ,這暫時理解不了
public static boolean isPowerOfFour2(int num) {

    return (num&(num-1))==0 && num>0 && (num-1)%3==0;

}

git:https://github.com/woshiyexinjie/leetcode-xinleetcode

相關文章
相關標籤/搜索