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. css position, display, float 内联元素、块级元素

    position属性:position属性指出一个元素的定位方法.有4种可能值:static, relative, absolute or fixed: static:默认值,元素按照在文档流中出现的 ...

  2. T138

    这一列车. 十年前送我去西安, 十年后搭我返故乡. 十年前手拉着手儿, 十年后独对着车窗.   这一列车. 装饰着坚毅的中国蓝, 却失去了往日光环. 只有通往偏远.落后的地方, 只有没赶上高铁动车的行 ...

  3. Scrapy 爬虫

    Scrapy 爬虫 使用指南 完全教程   scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name ...

  4. vmware安装cent os 6.5 + oracle 11g xe + jboss eap 6.2 + weblogic 12c+ webshpere mq 7.5

    前言: mac系统发展速度确实很快,短短数年,mac os上已经能网银支付(中行.招行.工商.支付宝等均已全面支持mac os了),windows上的经典常用软件:qq.飞信.旺旺.有道词典.有道云笔 ...

  5. Caffe学习系列(7):solver及其配置

    solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 # caffe train --solver=*_slover ...

  6. [MetaHook] Surface hook

    Hook ISurface function. #include <metahook.h> #include <vgui/ISurface.h> using namespace ...

  7. 在windows下安装配置Ulipad

    在windows下安装配置Ulipad 今天推荐一款轻便的文本编辑器Ulipad,用来写一些小的Python脚本非常方便. Ulipad下载地址: https://github.com/limodou ...

  8. VS2013无法链接到TFS(Visual studio online),错误TF31001,TF31002

    TF31002: Unable to connect to VisualStudio Online with VS 2013 but I can using web access - Windows ...

  9. SublimeText3下的Python开发环境配置

    最近重装了机器,需要重新安装Python的开发环境,中间遇到了几个问题,特些记录一下,以防下次备忘. 从Python的网站下载安装Python,这个非常简单,没有什么值得说的,大家可以参考廖雪峰的这个 ...

  10. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...