Leetcode Power of two, three, four
Given an integer, write a function to determine if it is a power of two.
Hint:
- Could you solve it in O(1) time and using O(1) space?
最容易想到的方法是用n反复的去除以2,一直到n变成1。这期间如果出现不能被2整除的数,那么一开始的n就不是2的power.
class Solution(object):
def isPowerOfTwo(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0:
return False while n > 1:
if n % 2 == 1:
return False
else:
n = n/2
return True
但是为了满足O(1)的time and space complexity, 可以使用位操作。因为2的power变成二进制时就是第一位是1后面都是0。
2 = 10, 4 = 100,8 = 1000, ... 那么 n-1 除了第一位是0,其他全是1。使用 & (bitwise AND)
return n > 0 and n & (n -1) == 0
判断一个数是不是3的power,可以用同样的思路。
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n <= 0:
return False while n > 1:
if n % 3 > 0:
return False
else:
n = n/3
return True
判断一个数是不是4的power。由于要求不能使用loop。4^a - 1 = (2^a + 1)(2^a - 1)。这两个相邻的奇数中肯定有一个是3的倍数。
return num > 0 and num & (num - 1) == 0 and (num - 1)%3 == 0
Leetcode Power of two, three, four的更多相关文章
- [LeetCode] Power of Four 判断4的次方数
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...
- [LeetCode] Power of Three 判断3的次方数
Given an integer, write a function to determine if it is a power of three. Follow up:Could you do it ...
- [LeetCode] Power of Two 判断2的次方数
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...
- LeetCode Power of Three
原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...
- Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...
- leetcode power(x,n)
class Solution { public: double pow(double x, int n) { double a=1; if(n==0)return 1; if(x==1)return ...
- LeetCode——Power of Two
Description: Given an integer, write a function to determine if it is a power of two. public class S ...
- [LeetCode]Power of N
题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...
随机推荐
- js点击左右滚动+默认自动滚动类
js点击左右滚动+默认自动滚动类 点击下载
- PAT 1026. 程序运行时间(15)
要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick,即&q ...
- Jquery操作下拉框(DropDownList)实现取值赋值
Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...
- mybatis 3.2.7 与 spring mvc 3.x、logback整合
github上有一个Mybatis-Spring的项目,专门用于辅助完成mybatis与spring的整合,大大简化了整合难度,使用步骤: 准备工作: maven依赖项: <properties ...
- 2014-2015-2 《Java程序设计》课程学生博客列表
20135101 曹钰晶 20135103 王海宁 20135104 刘 帅 20135105 王雪铖 20135109 高艺桐 20135111 李光豫 20135114 王朝宪 20135116 ...
- 对atime、mtime和ctime的研究
前期准备 在实验之前我们在讨论为何会出现两种修改时间,为此我们推测因为修改的不是文件的同一数据,或者说同一地方,那么我们就要先搞清楚文件的结构. linux文件系统是Linux系统的心脏部分,提供了层 ...
- FineUI v3.3.1 发布了!
关于FineUI基于 ExtJS 的专业 ASP.NET 控件库. FineUI的使命创建 No JavaScript,No CSS,No UpdatePanel,No ViewState,No We ...
- centos设置静态IP
1.编辑网卡文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 # eth0为网卡编号 设置网卡eth0的IPV4信息,需要注意的是,设置的IPADDR需要 ...
- Google最新截屏案例详解
Google从Android 5.0 开始,给出了截屏案例ScreenCapture,在同版本的examples的Media类别中可以找到.给需要开发手机或平板截屏应用的小伙伴提供了非常有意义的参考资 ...
- Spring学习进阶 (三) Spring AOP
一.是什么AOP是Aspect Oriented Programing的简称,最初被译为“面向方面编程”:AOP通过横向抽取机制为无法通过纵向继承体系进行抽象的重复性代码提供了解决方案.比如事务的控制 ...