[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 ...
随机推荐
- jupyter notebook使用时路径问题和kernel error,安装opencv
修改路径: 在C:\Users\Administrator\ .jupyter 目录下面只有一个“migrated”文件. 打开命令窗口(运行->cmd),进入python的Script目录下输 ...
- Log4Net 之走进Log4Net (四)
原文:Log4Net 之走进Log4Net (四) 一.Log4net的结构 log4net 有四种主要的组件,分别是Logger(记录器), Repository(库), Appender(附着器) ...
- 多个nginx之间如何实现反向代理和负责均衡
1)nginx反向代理: http { upstream routeadmin { ip_hash; server 127.0.0.1:9201 weight= ...
- ISC2016训练赛 phrackCTF--Classical CrackMe
测试文件:https://static2.ichunqiu.com/icq/resources/fileupload/phrackCTF/REVERSE/CrackMe.rar 1.准备 获得信息 3 ...
- MySQL语句之数据的增删改查
1.插入记录insert语法:INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn); 也可以一次 ...
- Node 12 值得关注的新特性
前言 时隔一年,Node.js 12 如约而至,正式发布第一个 Current 版本. 该版本带来了诸如: V8 更新带来好多不错的特性. HTTP 解析速度提升. 启动速度大幅提升. 更好的诊断报告 ...
- Linux性能优化从入门到实战:07 CPU篇:CPU性能优化方法
性能优化方法论 动手优化性能之前,需要明确以下三个问题: (1)如何评估性能优化的效果? 确定性能的量化指标.测试优化前的性能指标.测试优化后的性能指标. 量化指标的选择.至少要从应用程序 ...
- 【技巧】Windows 10 1809无法接收1903解决方法
这都7月份了,Windows10 1903都升级的有一个月了,然而我的1809的系统一直找不到1903的更新. 虽说1903会有bug,但还是想体验一把.周围同事都更新了,心里还是痒痒的. 于是每天都 ...
- less:注释
less有两种注释方法,一种是// 一种是/**/ 这两者的区别是/**/是会被编译成css里面,而//则不会 举个例子(使用的less在线编译器) 我们看到//注释并没有编译到css里面
- php 输出缓冲
<?php ob_start();//开启php输出缓冲区 echo "A"; //"A"会进入php输出缓冲区 ob_flush();//将php输出缓 ...