题目:

Given an integer, write a function to determine if it is a power of two.

class Solution(object):
def isPowerOfTwo(self, n):
#
"""
:type n: int
:rtype: bool
"""

方法:分析2的幂次方的特点,发现2的任意次方的数,化成二进制时只有首位为1其余位为0,因此我的解决方法如下:

class Solution(object):
def isPowerOfTwo(self, n):
return False if n<0 else (True if bin(n).count('')==1 else False)

bin函数能将一个给定的数化成二进制,返回为字符串形式,因此只要检查bin的返回值中是否含有一个‘1’即可。

将上面的一行代码分解来看就是

class Solution(object):
def isPowerOfTwo(self, n):
if n<0:
return False
return True if bin(n).count('')==1 else False

之后看其中其他解决方法主要为n&(n-1)==0,这个方法也是利用了2的幂次方的特点,n若是2的幂次方,那么(n-1)的二进制数只有最高位为0,其余位全为1,因此让n&(n-1)按位与,如果等于0,则说明n是2的幂次方

python leetcode 日记--231. Power of Two的更多相关文章

  1. 【LeetCode】231. Power of Two 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二进制 位运算 判断是不是最大2的幂的因数 判断因子 ...

  2. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  3. 【一天一道LeetCode】#231. Power of Two

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  5. 【LeetCode】231 - Power of Two

    Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的 ...

  6. python leetcode 日记--Maximal Square--221

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  7. 【一天一道LeetCode】#342. Power of Four

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  9. LeetCode 第 231 题 (Power of Two)

    LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...

随机推荐

  1. vs extension

    优先级高低

  2. s3c2440 移值u-boot-2016.03 第4篇 支持NAND flash 识别

    1, /include/configs/smdk2440.h 中添加 #define CONFIG_CMD_NAND 编译 drivers/mtd/nand/built-in.o: In functi ...

  3. Linux phpwind论坛的安装

    1:新建文件夹phpwind

  4. 一步一步教你编写与搭建自动化测试框架——python篇

    [本文出自天外归云的博客园] 这两天用python写了一个自动化测试框架,取名为Auty.准备用来做Web方面的接口测试,以下为Auty框架一步一步的搭建过程——

  5. android 事件监听

    步骤: 1.获取代表控件对象. 2.定义一个类,实现监听接口. 3.生成监听器对象. 4.为控件绑定监听器对象. XML <LinearLayout xmlns:android="ht ...

  6. 关于Extjs MVC模式上传文件的简单方式

    Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...

  7. IPV6

    (一) iOS 9.0.OS X 10.11 以上的系统 在IPv6的环境下 是支持IP地址访问网络的.所以大家测试机如果是 iOS9.0以上的系统,可以直接通过IP访问.这是因为iOS 9.0之后  ...

  8. Shiro Security

    手动创建shiro Filter的java代码 // Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecu ...

  9. php总结 --- 2.字符串

    字符串 恩聪 正则表达式 恩聪 中文验证 if (preg_match("/[\x{4e00}-\x{9fa5}]{2,4}\s{0,}\w{8,15}/u", $keyword) ...

  10. [http session]

    原文链接:http://lavasoft.blog.51cto.com/62575/275589/ 1.Session创建的时间是: 一个常见的误解是以为session在有客户端访问时就被创建,然而事 ...