问题描述:

给定一个整数,编写一个函数来判断它是否是 2 的幂次方。

示例 1:

输入: 1
输出: true
解释: 2

0

 = 1

示例 2:

输入: 16
输出: true
解释: 2

4

 = 16

示例 3:

输入: 218
输出: false

方法1:

 import math
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n % 2 != 0 and n != 1 or n < 0 :
return False
width = int((math.sqrt(n)))
for i in range(width+2):
if math.pow(2,i) == n:
return True
elif math.pow(2,i) > n:
return False
return False

方法2:二进制

 class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0:
return False return bin(n).count('') == 1

方法3:

 class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
while n%2 == 0 and n>1:
n = n/2
return (n==1)

2018-09-20 06:58:15

LeetCode--231--2的幂函的更多相关文章

  1. LeetCode 231.2的幂

    LeetCode 231.2的幂 题目: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 算法: 若一个数是2的幂次的话定会有n & (n - 1) == 0这个关系成立 所以直接用 ...

  2. [LeetCode] 231. 2 的幂

    位运算 231. 2 的幂 ``` class Solution { public boolean isPowerOfTwo(int n) { int cnt = 0; while (n>0) ...

  3. Java实现 LeetCode 230 2的幂

    231. 2的幂 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = ...

  4. leetcode刷题笔记231 2的幂

    题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为 ...

  5. 力扣(LeetCode)231. 2的幂

    给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = 16 示例 3: ...

  6. [LeetCode] 231. Power of Two ☆(是否2 的幂)

    描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...

  7. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

  8. [LeetCode]231. Power of Two判断是不是2\3\4的幂

    /* 用位操作,乘2相当于左移1位,所以2的幂只有最高位是1 所以问题就是判断你是不是只有最高位是1,怎判断呢 这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了 如果是 ...

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

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

  10. LeetCode 231 Power of Two

    Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...

随机推荐

  1. git初学

    git在团队合作开发时是很有用的,SVN是集中式的代表,而git是分布式的代表,它分为代码区.暂存区.和本地库.在同一个团队中开发时,在代码存储中心(例如,码云.github)上创建一个库,用于储存和 ...

  2. CSS层叠样式表--找到标签

    0 怎么学习CSS 1 CSS的四种引入方式 2 CSS的四种基本选择器 3 属性选择器 4 CSS伪类 5 CSS选择器优先级 6 CSS的继承性 怎么学习CSS 1.怎么找到标签(CSS选择器) ...

  3. bzoj 1251: 序列终结者 平衡树,fhqtreap

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1251 思路 好简单的模板题 不过还是wrong了好几发 叶子节点要注意下,不能使用 遇到就不 ...

  4. Linux/shell: Concatenate multiple lines to one line

    $ cat file START Unix Linux START Solaris Aix SCO 1. Join the lines following the pattern START with ...

  5. 数据库03_SQL语句

    由于在笔试中遇到写sql语句的题目,犯了低级错误,这里学习并总结一下,遇到相关的继续更新... 数据定义 1.创建数据库 create database testdb; 2.创建空表 需要指明表明.字 ...

  6. C# this.Invoke和this.BeginInvoke 最简单的写法

    https://blog.csdn.net/gtosky4u/article/details/20118813 this.BeginInvoke(new EventHandler(delegate { ...

  7. P4980 【模板】Polya定理

    思路 polya定理的模板题,但是还要加一些优化 题目的答案就是 \[ \frac{\sum_{i=1}^n n^{gcd(i,n)}}{n} \] 考虑上方的式子怎么求 因为\(gcd(i,n)\) ...

  8. Component 组件props 属性设置

    props定义属性并获取属性值 html <div id="app"> <!-- 注册一个全局逐渐 --> <!-- 注意如果自定义的属性带-像下面这 ...

  9. pgAdmin的数据恢复

    DOC 本地添加server 1.设置备份.恢复的exe路径.一般在pgAdmin的安装路径下可以找到 2.恢复restore,备份backup

  10. [转载]Linux中的网络接口及LO回环接口

    转自:https://blog.csdn.net/weixin_39863747/article/details/80564358 Linux中的网络接口及LO回环接口 2018年06月04日 10: ...