LeetCode--231--2的幂函
问题描述:
给定一个整数,编写一个函数来判断它是否是 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的幂函的更多相关文章
- LeetCode 231.2的幂
LeetCode 231.2的幂 题目: 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 算法: 若一个数是2的幂次的话定会有n & (n - 1) == 0这个关系成立 所以直接用 ...
- [LeetCode] 231. 2 的幂
位运算 231. 2 的幂 ``` class Solution { public boolean isPowerOfTwo(int n) { int cnt = 0; while (n>0) ...
- Java实现 LeetCode 230 2的幂
231. 2的幂 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = ...
- leetcode刷题笔记231 2的幂
题目描述: 给定一个整数,写一个函数来判断它是否是2的幂. 题目分析: 判断一个整数是不是2的幂,可根据二进制来分析.2的幂如2,4,8,等有一个特点: 二进制数首位为1,其他位为0,如2为10,4为 ...
- 力扣(LeetCode)231. 2的幂
给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = 16 示例 3: ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- 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的 ...
- [LeetCode]231. Power of Two判断是不是2\3\4的幂
/* 用位操作,乘2相当于左移1位,所以2的幂只有最高位是1 所以问题就是判断你是不是只有最高位是1,怎判断呢 这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了 如果是 ...
- [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
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
随机推荐
- freeswitch控制台日志级别设置以及存储
1.在管理控制台上设置 console loglevel (0~7)越往上级别越大 2.打开sip详细日志 sofia profile internal siptrace on 3.关闭sip详细日志 ...
- SSM集成activiti6.0错误集锦(一)
项目环境 Maven构建 数据库:Orcle12c 服务器:Tomcat9 <java.version>1.8</java.version> <activiti.vers ...
- js的原型prototype究竟是什么?
Javascript也是面向对象的语言,但它是一种基于原型Prototype的语言,而不是基于类的语言.在Javascript中,类和对象看起来没有太多的区别. 1.什么是prototype: fun ...
- (zhuan) Attention in Long Short-Term Memory Recurrent Neural Networks
Attention in Long Short-Term Memory Recurrent Neural Networks by Jason Brownlee on June 30, 2017 in ...
- Unity3D学习笔记(二十九):AssetBundle
AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...
- FPGA 概述2
参考1 参考2:浅论各种调试接口(SWD.JTAG.Jlink.Ulink.STlink)的区别 以下数据仅供参考 文章概要 主流FPGA厂商及产品 相同设计在FPGA与ASIC中耗费器件数量比较 F ...
- 1、http协议基础及IO模型
Nginx (web server,web reverse proxy): http协议:80/tcp,HyperText Transfer Procotol http协议版本: HTTP/0.9:原 ...
- 【译】第20节---数据注解-InverseProperty
原文:http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-co ...
- 解决:springmvc中接收date数据问题
这里提供三种解决方案. 一.局部转换 :只是对当前Controller类有效 springMVC.xml中添加: <bean class="org.springframework.we ...
- vi/vim 基本使用方法
vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...