python leetcode 日记--231. Power of Two
题目:
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的更多相关文章
- 【LeetCode】231. Power of Two 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二进制 位运算 判断是不是最大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 ...
- 【一天一道LeetCode】#231. Power of Two
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- 【LeetCode】231 - Power of Two
Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的 ...
- 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 ...
- 【一天一道LeetCode】#342. Power of Four
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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)
LeetCode 第 231 题 (Power of Two) Given an integer, write a function to determine if it is a power of ...
随机推荐
- C#随学随记
1.Microsoft.NET是基于Windows平台的一种技术(简称.NET),它包含了能在.NET Framework平台运行的所有语言..NET Framework是微软为开发应用程序创建的一个 ...
- windows平台(不包括ARM的CE)通用的压缩和解压缩
通用是相对的,这里指的是xp和win7(其他版本我没测试过,不要用不要来找我) #define CMP_FRM COMPRESSION_FORMAT_LZNT1|COMPRESSION_ENGINE_ ...
- openDatabase() chrome vivaldi Stylish
located at /Users/ruili/Library/Application Support/Vivaldi/Default/databases/ Databases.db contains ...
- Black World
- Auty自动化测试框架第四篇——生成测试结果报告
[本文出自天外归云的博客园] 本次为Auty框架添加生成测试结果报告功能,文件结构更新:
- mac攻略(二) -- 简单配置php开发环境
最简单直接的方式还是使用 Mac 上自带的 Apache 和 PHP. 1.启动 Apache 1>启动apache $sudo apachectl start; 2>启动后,在浏览器 ...
- prototype与原型链
1.今天翻看 阮一峰老师的博客看到了,一篇讲javascript为什么要设计出prototype,跳转 大意就是new 的方式有缺陷,没有共同的属性,一下明白了很多. 在来一张原型链的图:
- Android的UI设计
一.Android控件 1.TextView 属性:id.width.height.gravity(对齐方式).textSize(文字大小).textColor(文字颜色) 2.Button 属性:i ...
- 十八、AWT绘图技术
1.Graphics 实现各类图形.文本和图片的绘制操作. 2.绘图颜色和笔画属性 (1)颜色属性 Color col= new Color(int r,int g,int b) Color col ...
- 【树莓派】树莓派网络配置:静态IP、无线网络、服务等
一.网络配置之静态IP: 树莓派的默认网络为: haochuang@raspberrypi:~ $ vi /etc/network/interfaces # interfaces() file use ...