题目描述:

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的更多相关文章

  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. 【leetcode❤python】342. Power of Four

    #-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfFour(self, num):        ""& ...

  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] 342. Power of Four(位操作)

    传送门 Description Given an integer (signed 32 bits), write a function to check whether it is a power o ...

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

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

  8. [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: ...

  9. 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 ...

随机推荐

  1. IOS第三方地图-百度地图集成

    百度地图官网: http://developer.baidu.com/map/index.php?title=iossdk 照上面吧百度地图sdk集成到工程 然后在pilst文件中加入: 如果地图没有 ...

  2. UVA 11133 - Eigensequence DP

    Given an increasing sequence of integers a1, a2, a3, . . . , ak, the E-transform produces a sequence ...

  3. js之数组常见的方法

    主要介绍数组的一些常用的方法,方法多了,就容易混淆,结果就是方法用错,甚至不会用: 一.数组的定义: 1.字面量/直接量: var arr = [1, 2, 'js', 'java']; 2.通过内部 ...

  4. MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]

    引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...

  5. 在Unity中使用贝塞尔曲线(转)

    鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天因为工作的原因需要将贝塞尔曲线加在工程中,那么MOMO迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的任意角度的曲线,这两个点一个是 ...

  6. ibatis2.3中#和$符号的区别(转)

    在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如: select * from ...

  7. 分布式 Key-Value 存储系统:Cassandra 入门

    Apache Cassandra 是一套开源分布式 Key-Value 存储系统.它最初由 Facebook 开发,用于储存特别大的数据. Cassandra 不是一个数据库,它是一个混合型的非关系的 ...

  8. Ubuntu--服务器版本系统安装图解教程

    附Ubuntu Server 13.04系统镜像下载地址: 32位:http://mirrors.163.com/ubuntu-releases/13.04/ubuntu-13.04-server-i ...

  9. Android百度地图开发05之公交信息检索 + 路线规划

    在上一篇blog中介绍过POI检索的使用,本篇blog主要介绍公交信息检索和线路规划的内容. 公交信息检索 实际上,公交信息检索与POI检索.在线建议检索非常相似,也是把你需要检索的信息发送给百度地图 ...

  10. ADB调试桥安装(方式二)

    想使用ADB工具可以通过安装安卓SDK套件,然后通过SDK里面的adb工具连接手机进行调试, 然而这种方式安装起来多多少少还是有点麻烦,ADB调试桥安装(方式一). 另一种方式来的就更为舒服一些了,即 ...