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的幂,不能使用循环和递归:
前面我们已经做过一个数是否为2的幂这道题,4的幂跟2的幂一样,都是最高位为1,其他位为0。
不同的是4的幂中的1在偶数位上,所以我们可以用0xaaaaaaaa与之按位与。
class Solution {
public:
bool isPowerOfFour(int num) {
return num>&&(!(num&(num-)))&&(!(num&(0xaaaaaaaa)));
}
};
342. Power of Four的更多相关文章
- 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] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- 342. Power of Four(One-line)
342. Power of Four Total Accepted: 707 Total Submissions: 2005 Difficulty: Easy Given an integer ...
- 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的幂数. ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
随机推荐
- Robotium solo.goBack();不起作用,解决方案
今天碰到用solo.goBack();点击手机的返回按键五次有四次起总用.查了半天后发现以下一种替代方法: solo.sendKey(KeyEvent.KEYCODE_BACK); robotium下 ...
- 微软开放技术发布开源的微软云服务器底盘管理器 (Chasis Manager) 软件
发布于 2014-07-14 作者 陈 忠岳 今天,微软公司加入开放计算项目(OCP),贡献出硬件和软件规范,管理 API 和协议,机械 CAD 模型,以及电路板文件和 Gerbers(描述印刷 ...
- 2015第42周六Pgsql全文索引
全文搜索通常也就是文本搜索,它可以提供满足查询的识别自然语言的能力,并且任意性地通过相关性查询进行排序.搜索最常见的类型就是找到所有包含给定的查询术语的记录,并且以相似性的查询顺序返回它们. 对于普通 ...
- VGA IP核的制作
今天看了本<系统晶片设计-使用NIOS>这本书,看到VGA IP核的设计不错,特移植到Cyclone III上来,试验一下效果. 顶层代码:binary_VGA.v module bina ...
- HDOJ/HDU 1039 Easier Done Than Said?(字符串处理~)
Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy ...
- 神器 Sublime Text 3 的一些常用快捷键
选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个例子:快速选中并更改所有相同的变量名. ...
- 文件夹oradiag_是如何产生的
如果sqlnet.ora不可用或者ADR_BASE参数未定义,那么11g的 SQL*Net将创建这些文件夹 (详情:http://download.oracle.com/docs/cd/B28359_ ...
- ubuntu错误解决。
ubuntu中出现如下错误: W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/main/i ...
- linux平台MongoDB数据库安装
跟Ruiy哥一起玩转吧; <一,初始化玩转MongoDB> 1,关闭SElinux(Ruiy哥根据经验知红帽的SElinux架设就是个错误,还记得不管啥结构首先要关闭的就是它); 2,设置 ...
- 手把手教你mysql(十)索引
手把手教你mysql(十)索引 一:索引的引入 索引定义:索引是由数据库表中一列或者多列组合而成,其作用是提高对表中数据的查询速度. 类似于图书的目录,方便快速定位,寻找指定的内容,如一本1000页的 ...