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. 题意:判断一个数是否是 ...
随机推荐
- 用Metasploit破解Mysql用户名和密码
下面这个方式是普适的,但缺点就是必须要有自己的用户名和密码字典.其原理就是用user.txt与pass.txt的两个文本去不停交叉验证. msf auxiliary(mysql_login) > ...
- javascript实现优先队列
1.概念 一般情况下从队列中删除元素,都是率先入队的元素.但是有些使用队列的情况不遵循先进先出的原则,这就是插队,这需要使用优选队列的数据结构来进行描述. 从优先队列中删除元素的时候,需要考虑优先级的 ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
- android 获取当前系统时间
取得系统时间 1. long time=System.currentTimeMillis(); 2. final Calendar mCalendar=Calendar.getInstance(); ...
- jquery 实现邮箱输入自动提示功能
邮箱的广泛使用得益于它的免费,因此很多网站在注册的时候都会直接使用邮箱作为账号名 为了提高用户的体验,很多网站都会实现邮箱输入的自动提示功能,所有自己也实现了一个,先看下效果吧,觉得效果还行的就拿去 ...
- unix环境高级编程基础知识之第二篇(3)
看了unix环境高级编程第三章,把代码也都自己敲了一遍,另主要讲解了一些IO函数,read/write/fseek/fcntl:这里主要是c函数,比较容易,看多了就熟悉了.对fcntl函数讲解比较到位 ...
- StackExchange.Redis--纯干货喂饱你
Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...
- JAVA GC 简单总结
GC分代 GC的英文全拼是Garbage Collection,意思是垃圾收集. Java 将堆内存分为三代来管理: - 年轻代 (Young Generation) - 年老代 (Old Gener ...
- LCA倍增算法的错误与模板
先上我原来的错误的代码 type node=^link; link=record num:int64; next:node; end; var fa:..,..] of int64; dep:..] ...
- yii2权限控制rbac之详细操作步骤
本篇的主题是 rbac权限控制的详细操作步骤,注意是操作步骤哦,关于配置与rbac的搭建,我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程说的再清楚不过了. 但是,在很多人的反馈下,说是 ...