231. Power of Two
题目:
Given an integer, write a function to determine if it is a power of two.
链接: http://leetcode.com/problems/power-of-two/
题解:
验证一个数是否是2的幂次,我们可以使用(n & (n - 1))来决定。 比如 10 & 01, 100 & 011等等。
Time Complexity - O(1), Space Complexity - O(1)
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n <= 0)
return 0;
return (n & (n - 1) == 0);
}
}
二刷:
二的幂有一个特点,就是整个数字里只有一个比特位为1,其余都是0, 我们可以count有几个比特位为1,大于1的话结果为false,等于1的话为true
Java:
Time Complexity - O(1), Space Complexity - O(1)
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n < 1) {
return false;
}
int count = 0;
while (n != 0) {
n &= n - 1;
count++;
}
return count == 1;
}
}
更简练的写法 - 因为2的幂次只有一个比特位为1,那么我们用n & (n - 1)将其清零后,得到的结果应该是0。
public class Solution {
public boolean isPowerOfTwo(int n) {
if (n < 1) {
return false;
}
return (n & (n - 1)) == 0;
}
}
Reference:
https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
231. Power of Two的更多相关文章
- 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂
231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- LeetCode - 326, 342, 231 Power of Three, Four, and Two
1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LN : leetcode 231 Power of Two
lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...
- LeetCode 231 Power of Two
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- (easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...
- 【LeetCode】231 - Power of Two
Given an integer, write a function to determine if it is a power of two. Solution:一个整数如果是2的整数次方,那么它的 ...
随机推荐
- Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo
1.新建Dll工程 2.Dll工程全部代码 library SubMain; { Important note about DLL memory management: ShareMem must b ...
- 移植Oracle procedure 到 postgresql
1.登录postgresql psql -h 192.168.137.131 -p 5432 postgres satusc@6789#JKL 2.创建用户 CREATE USER name thun ...
- WPF 使用定时器
WPF 使用定时器:<ProgressBar Height="10" HorizontalAlignment="Left" Margin="28 ...
- Infobright高性能数据仓库
1. 概述 Infobright是一款基于独特的专利知识网格技术的列式数据库.Infobright简单易用,快速安装部署,使用中无需复杂操作,能大幅度减少管理工作:在应对50TB甚至更多数据量进行多 ...
- verilogHDL设计中的同步时序逻辑
引用自夏宇闻教授 1.同步时序逻辑: 是指表示状态的寄存器组的值只能在唯一确定的触发条件发生改变. 只能由时钟的正跳变沿或者负跳变沿触发的状态机就是一例,always@(posedge clk). 1 ...
- 仿微博视频边下边播之滑动TableView自动播放-b
Tips:这次的内容分为两篇文章讲述01.[iOS]仿微博视频边下边播之封装播放器 讲述如何封装一个实现了边下边播并且缓存的视频播放器.02.[iOS]仿微博视频边下边播之滑动TableView自动播 ...
- org.apache.commons.dbutils.QueryRunner 执行sqlserver的存储过程
执行不带输出参数的存储过程与 执行普通update sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...
- java和javascript真的有关系=。=
相同点:1. 内存管理,两者都采用GC来对内存进行回收.因此Java与javascript的内存泄露情况十分相似. 2. 代码编译为机器码后由中间件执行:Java使用前会编译为字节码后由JVM执行,V ...
- linux源码阅读笔记 fork函数
在阅读源码的过程中,发现找不到fork函数的定义.后来在linux/init/main.c中找到了这样一条语句 static inline _syscall0(int,fork) 原来这里就是fork ...
- Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题
Ubuntu 自带的Remmina Remote Desktop 用来连接windows,vnc,ssh等非常方便好用, 但我在连接windows 2008 r2 server时遇到一个问题: ...