[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的幂次。
题解:我们知道凡是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(位操作)的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode 342 Power of Four 数论
题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- [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: ...
- 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 ...
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
随机推荐
- BZOJ 1085(IDA*)
题面 传送门 分析 首先,直接搜索肯定会TLE 很容易想到用迭代加深的方法,限定搜索深度 但是,这样仍然不够,需要用启发式的方法优化 我们设计一个估价函数f(x)=g(x)+h(x)f(x)=g(x) ...
- 关于使用itext转Html为pdf添加css样式的问题
使用的jar文件 xmlworker-5.5.11.jar itextpdf-5.5.11.jar 下载地址:https://pan.baidu.com/s/1i5AIBvZ 以下为测试代码 pack ...
- WAF防火墙学习
正则解析神器 http://rick.measham.id.au/paste/explain.pl http://regexr.com/ http://regex101.com/ http://www ...
- PTA 错题记录
程设期中考, 记录一下曾经做错的选择填空. 1. 2. 3. 4. 5. 6.
- SpringBoot application.proerties基本配置
#设置日志输出路径,日志文件名称,输出日志级别 默认日志文件超过10M会切分成多个文件 最新的日志文件就是设置的日志文件 logging.level.root=INFOlogging.level.or ...
- alert(1) to win 14
<!--<script></script>之间的内容会被当作js处理,所以,//we'll use this later </script>被注释了.最终 i ...
- HTML5 Canvas(实战:绘制饼图2 Tooltip)
继上一篇HTML5 Canvas(实战:绘制饼图)之后,笔者研究了一下如何给饼图加鼠标停留时显示的提示框. Plot对象 在开始Coding之前,笔者能够想到的最easy的方式,就是给饼图的每一个区域 ...
- java 关键字volatile
一.Java内存模型 想要理解volatile为什么能确保可见性,就要先理解Java中的内存模型是什么样的. Java内存模型规定了所有的变量都存储在主内存中.每条线程中还有自己的工作内存,线程的工作 ...
- pspice建立仿真模型元件库
摘自:http://royroyyy.blog.163.com/blog/static/1376506172011026102216175/ PSpice9.2电子元器件模型--由网页下载的model ...
- rocketmq启动broker内存占用过大的问题
解决方法: 修改broker启动脚本runbroker.sh里面的jvm参数 JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g" ...