0
点赞
收藏
分享

微信扫一扫

LeetCode 342. Power of Four (Java版; Easy)


​​welcome to my blog​​

LeetCode 342. Power of Four (Java版; Easy)

题目描述

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

Example 1:

Input: 16
Output: true
Example 2:

Input: 5
Output: false
Follow up: Could you solve it without loops/recursion?

第一次做; 核心: 如果x是4的幂, 那么x满足(Math.log(num)/Math.log(2) % 2 ==0)

class Solution {
public boolean isPowerOfFour(int num) {
return num>0 && (Math.log(num)/Math.log(2) % 2 ==0);
}
}

// x是否是2的幂, x>0 && x&(x-1)==0;


举报

相关推荐

0 条评论