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 isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
if num & (num - 1) != 0:
return False
if num & 0x55555555 == 0:
return False return True
Python [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 ...
- 【leetcode❤python】342. Power of Four
#-*- coding: UTF-8 -*- class Solution(object): def isPowerOfFour(self, num): ""& ...
- 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] 342. Power of Four(位操作)
传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...
- [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 ...
随机推荐
- awk处理之案例三:awk去掉不需要的文本行
编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...
- poj 3621(最优比率环)
题目链接:http://poj.org/problem?id=3621 思路:之前做过最小比率生成树,也是属于0/1整数划分问题,这次碰到这道最优比率环,很是熟悉,可惜精度没控制好,要不就是wa,要不 ...
- jmeter 异步子请求测试随笔
好久没写技术类的博客了,都不知道自己都在忙啥.... 最近陆续遇到了一些异步子请求的测试需求,比如打开某一个页面A,A页面里的js会再调用B,C,D,E等请求,针对这个页面的测试,我最近做了一些思考: ...
- mysql变量使用总结
set语句的学习: 使用select定义用户变量的实践将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties);我的修改: sele ...
- asyn4j -- java 异步方法调用框架
asyn4j 是一个java异步方法调用框架,基于消费者与生产者模式.包括了异步方法执行,异步回调执行,异步工作缓存模块.支持Spring. 让我们写异步方法不再写很多的相关多线程代码.用asyn4j ...
- IIS7 ASP.NET 未被授权访问所请求的资源
IIS7 ASP.NET 未被授权访问所请求的资源 ASP.NET 未被授权访问所请求的资源.请考虑授予 ASP.NET 请求标识访问此资源的权限. ASP.NET 有一个在应用程序没有模拟时使用的基 ...
- Java-J2SE学习笔记-树状展现文件结构
1.利用java.io相关类树状展现文件结构 2.判定给定路径是否为dir,是则递归,每一递归一层缩进一次 3.代码 package Test; import java.io.File; public ...
- Pipeline aborted due to error
报错内容 x. Pipeline aborted due to error {:exception=>"LogStash::ConfigurationError", ... ...
- Android 实现Path2.0中绚丽的的旋转菜单
上图先: 那么下面开始吧~ 首先,将整个菜单动画分解开来. 1. 一级菜单按钮的旋转动画2个,十字和叉叉状态的转换. 2. 二级菜单按钮的平移动画2个,弹簧效果的in和out ...
- 【Tech】Ganglia安装配置
基础配置: Hadoop 2.2.0,Hbase 0.96. 四台集群机器,一台master,三台slave. 三台slave上分别装gmond:namenode机器上设置datasource. 客户 ...