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;
发布了580 篇原创文章 · 获赞 130 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/littlehaes/article/details/104226025