Given an integer, write a function to determine if it is a power of two.

Hint:

  1. 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  5. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  6. Leetcode Power of Two

    Given an integer, write a function to determine if it is a power of two. 题目意思: 给定一个整数,判断是否是2的幂 解题思路: ...

  7. 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 ...

  8. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...

  9. [LeetCode]Power of N

    题目:Power of Two Given an integer, write a function to determine if it is a power of two. 题意:判断一个数是否是 ...

随机推荐

  1. smarty foreach循环

    1,smarty foreach1,单纯的数组array(1000,2000,3000),使用foreach(from = $array item=foo){$foo}2,键值对数组<ul> ...

  2. JS/JQ常见兼容辅助插件

    1.Respond.js Respond.js 是一个快速.轻量的 polyfill,用于为 IE6-8 以及其它不支持 CSS3 Media Queries 的浏览器提供媒体查询的 min-widt ...

  3. struts2: config-browser-plugin 与 convention-plugin 学习

    struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了.convention-plugin. config-browser-plugin这二个 ...

  4. java:利用xpath删除xml中的空节点

    原始xml内容: <data> <a> </a> <b>b1</b> <awb> <awbpre>123</a ...

  5. Hibernate3.3.2 手动配置annotation环境

    简单记录Hibernate3.3.2如何快速配置环境 一.下载hibernate-distribution-3.3.2.GA-dist.zip文件,建立User libraries. 打开window ...

  6. 数据库MySQL与Oracle的一些去O注意项

    一.oracle递归查询语句start with ...connect by prior ① 给你一张表,表里面有主键id,以及该项的父节点parent_id,查询出该表中所有的父子关系节点树? Or ...

  7. 20145230GDB调试汇编堆栈过程分析

    20145230GDB调试汇编堆栈过程分析 分析过程 出现的问题:一开始无法编译,是因为我们的Linux中没有安装一个库. 进入之前先设置断点,之后disassemble可以获取汇编代码,用i r指令 ...

  8. SQL Server 2012新特性(1)T-SQL操作FileTable目录实例

    在SQL Server 2008提供FileStream,以借助Windows系统本身的API来强化SQL Server对于非结构化数据的支持后,SQL Server 2012更是推出了像Contai ...

  9. matlab 中randn randi rand randsrc的用法以及区别

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数                      rand(m,n,'double') ...

  10. 东大OJ-Prim算法

    1222: Sweep the snow 时间限制: 1 Sec  内存限制: 128 MB 提交: 28  解决: 18 [提交][状态][讨论版] 题目描述 After the big big s ...