[LeetCode]--342. Power of Four

简介: 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.Follow up: Could you solve it

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.

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

Credits:
Special thanks to @yukuairoy for adding this problem and creating all test cases.

Java Math的floor,round,ceil函数小结

跟上面的题一样,只不过换成了四。

public class Solution {
    private static final double epsilon = 10e-15;

    public boolean isPowerOfFour(int num) {
        if (num == 0)
            return false;
        double res = Math.log(num) / Math.log(4);
        return Math.abs(res - Math.round(res)) < epsilon;
    }
}

另外一种常规方法。

public boolean isPowerOfFour(int n) {
        if (n == 0)
            return false;
        if (n == 1)
            return true;
        if (n % 4 == 0)
            return isPowerOfFour(n / 4);
        return false;
    }
目录
相关文章
LeetCode 342. Power of Four
给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。
77 0
LeetCode 342. Power of Four
|
Java C++
LeetCode之Power of Two
LeetCode之Power of Two
116 0
|
Java
[LeetCode]--326. Power of Three
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? Credits: Special thanks to @dietpepsi for addin
1111 0
[LeetCode]--231. Power of Two
Given an integer, write a function to determine if it is a power of two. Credits: Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. 如果是power of
1089 0
LeetCode 326 Power of Three(3的幂)(递归、Log函数)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50540517 翻译 给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适…… 跟进: 你是否可以不用任何循环或递归来完成。
785 0
LeetCode 231 Power of Two(2的幂)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50541137 翻译 给定一个整型数,写一个函数来决定它是否是2的幂。
702 0