传送门

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的幂次。

题解:我们知道凡是4的幂次,那么其二进制位肯定只有一个1其余都是0,并且可以发现规律,从4的零次幂开始列举,可以发现与上一个幂次相比,隔了一个0,也就是将4的幂次的二进制位写在一起,呈现出...0101...的形式,因此根据  num & (num - 1)可以判定二进制位是否只有一个1,然后再与十六进制的55555555相与即可判断是否有4的幂次的表现形式,因为十六进制的5的表现形式正好是0101。

class Solution {
public:
//3ms
bool isPowerOfFour(int num) {
return !(num & (num - 1)) && (num & 0x55555555);
}
};

  

[LeetCode] 342. Power of Four(位操作)的更多相关文章

  1. [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:Giv ...

  2. Python [Leetcode 342]Power of Four

    题目描述: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Examp ...

  3. 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:Giv ...

  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:Giv ...

  5. Leetcode 342 Power of Four 数论

    题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...

  6. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  7. [LeetCode] 231. Power of Two 2的次方数

    Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...

  8. 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 Sol ...

  9. leetcode 326. Power of Three(不用循环或递归)

    leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...

随机推荐

  1. C# xml格式字符串,插入到数据库出现非法字符

    在debug模式下快速监视看到的数据是完全正常的,即取到的是<xml>,但是把该字符串拷贝到UltraEdit中,取到的第一个字符是问号.使用正则表达式^[^<]进行替换,意思是把开 ...

  2. C#中ComboBox动态绑定赋值

    http://www.crifan.com/csharp_combobox_data_dynamic_binding/ C#中,已有一个List,想要动态的,绑定到ComboBox中. [解决过程] ...

  3. 灵活的理解JavaScript中的this指向(一)

    this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确 ...

  4. Linux终端复用工具tmux的使用和配置

    1. 会话管理 新建会话 $ tmux new -s session-one -d -s:指定回话名称 -d:会话在后台运行 查看所有会话 $ tmux ls session-one: 1 windo ...

  5. 前端开发JavaScript入门——JavaScript介绍&基本数据类型

    JavaScript 诞生于1995年,它的出现主要是用于处理网页中的 前端验证. • 所谓的前端验证,就是指检查用户输入的内容是否符合一定的 规则. • 比如:用户名的长度,密码的长度,邮箱的格式等 ...

  6. jQuery学习总结04-文档处理

    1.append(content|fn) 说明:向每个匹配的元素内部追加内容. 这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似. content(要追加到目标中的内 ...

  7. 【leetcode】1029. Two City Scheduling

    题目如下: There are 2N people a company is planning to interview. The cost of flying the i-th person to ...

  8. Task5.NB_SVM_LDA

    参考:https://blog.csdn.net/u013710265/article/details/72780520 贝叶斯公式就一行: P(Y|X)=P(X|Y)P(Y)P(X) 而它其实是由以 ...

  9. 16 :IDEA快速键

    ctrol+z ctrol+shift+z  重做 复制,粘贴,删除,(行操作,光标放在那里就可以操作,不要全选择) 注:特别:查询出来,文件是可以直接编辑的 crtol+F double +shif ...

  10. MapGIS IGServer for java

    但是安装完之后,服务里面没有找到igserver服务 IGServer SManager cxf 怎么会找不到类呢?是服务映射出了问题,所以目录找不到.所以怎么配置目录呢?是在xml还是环境变量,还是 ...