题目:

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. 从头开始一步一步实现EF6+Autofac+MVC5+Bootstarp极简的实现前后台ajax表格展示及分页实现

    本来是想试着做一个简单OA项目玩玩的,真是不做不知道,一做吓死人,原来以为很简单的事情,但是做起来不是忘这就是忘那的,有的技术还得重新温习.所以还是得记录.免得哪天电脑挂了,就全没有了. 开始是看了园 ...

  2. centos 更新python

    1.CentOS安装Python的依赖包 yum groupinstall "Development tools"yum install zlib-devel bzip2-deve ...

  3. 三个 DAL 相关的Java代码小工具

    最近在做 DAL (Data Access Layer 数据访问层) 的服务化,发现有不少地方是人工编写比较繁琐的,因此写了几个小工具来完成. 1.  从 DAO 类自动生成 CoreService ...

  4. wex5 教程之 图文讲解 文件上传attachmentSimple(1)

    视频教程地址:http://v.youku.com/v_show/id_XMTc4NDAyMTY4OA==.html 效果预览: 1 调用attchmentSimple组件,打开文件管理器,并选中,显 ...

  5. 让DIV水平和垂直居中的几种方法

    我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示.我们传统解决的办法是用纯CSS来让DIV居中.在本文中,我将给大家讲述如何用CSS和jQu ...

  6. Java 中System里getProperty(something)

    Java 中System里getProperty 方法获得系统参数 Key Description of Associated Value 中文描述 java.version Java Runtime ...

  7. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

  8. 【转】VS2010中 C++创建DLL图解

    转载地址:http://blog.csdn.net/g710710/article/details/7255744 一.DLL的创建  创建项目: Win32->Win32项目,名称:MyDLL ...

  9. GnuPG 的PGP使用

    1. 生成秘钥对(此处采用默认的RSA, 2048位) $ gpg --gen-key gpg (GnuPG) ; Copyright (C) Free Software Foundation, In ...

  10. ajax完整结构

    $.ajax({ url:"服务器", data:{"key":所传数据}, type:"post", dataType:"jso ...