LeetCode 342. Power of Four (4的次方)

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?



题目标签:Bit Manipulation

  这道题目让我们判断一个数字是不是4的次方数,首先排除负数和0。然后利用2的次方数特性,如果 num & (num-1) == 0 的话,那么这个数字是2的次方数 (基于2进制原理)。利用这一点,排除所有不是2的次方数。最后,如果一个数字是4的次方数,那么这个数字 -1 一定可以被3整除。排除掉数字 -1不能被3整除的,剩下的就是4的次方数

Java Solution:

Runtime beats 24.44%

完成日期:06/26/2017

关键词:Bit Manipulation

关键点:基于 num & (num-1) 来判断是不是2的次方数

 1 public class Solution
 2 {
 3     public boolean isPowerOfFour(int num)
 4     {
 5         if(num <= 0) // if num is 0 or negative number
 6             return false;
 7
 8         if((num & (num - 1)) != 0) // if num is not the power of 2
 9             return false;
10
11         if((num - 1) % 3 != 0) // if num - 1 cannot be divided by 3
12             return false;
13
14         return true;
15     }
16 }

参考资料:

http://www.cnblogs.com/grandyang/p/5403783.html

时间: 2024-07-30 13:53:02

LeetCode 342. Power of Four (4的次方)的相关文章

leetCode 342. Power of Four 位运算

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 without loops/recursion? 题目大意: 判断一个数是否为4的幂. 思路

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 without loops/recursion? 先说明用循环: 如下 class Solution { public: bool

Python [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. 解题思路: 位操作. 首先判断是不是只有一位数字为1,其余为0 然后判断为1的位置是不是奇数位 代码如下: class Solution(object): def isPower

[LeetCode] 342. Power of Four(位操作)

传送门 Description 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? 思路 题意:不使用循环和递归,求一个数是否是4的

Leetcode 342 Power of Four 数论

题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. 注意:判断一个双精度数a是否是0,请用fabs(a)<一个极小的数,如本题中用的是1e-9. 1 class Solution { 2 public: 3 bool isPowerOfFour(int num) { 4 return (num > 0 ) && (fabs((in

231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

231. Power of Two Given an integer, write a function to determine if it is a power of two. class Solution { public: bool isPowerOfTwo(int n) { return n > 0 ? (n & (n-1)) == 0 : false; } }; 342. Power of Four Given an integer (signed 32 bits), write

LeetCode 231 Power of Two(2的幂)

翻译 给定一个整型数,写一个函数来决定它是否是2的幂. 原文 Given an integer, write a function to determine if it is a power of two. 分析 详情请看这篇文章:LeetCode 326 Power of Three(3的幂)(递归.Log函数) 看题号,326是本题的加强版,326是要求不能用循环或递归的--大家可以去看看上面那篇文章. 本题就直接贴我的代码了-- 代码 class Solution { public: bo

342. Power of Four(One-line)

342. Power of Four Total Accepted: 707 Total Submissions: 2005 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. Follow up: Cou

【一天一道LeetCode】#342. Power of Four

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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. F