[LeetCode]231. Power of Two判断是不是2\3\4的幂
/*
用位操作,乘2相当于左移1位,所以2的幂只有最高位是1
所以问题就是判断你是不是只有最高位是1,怎判断呢
这些数-1后形成的数,除了最高位,后边都是1,如果n&n-1就可以判断了
如果是2的幂,&的结果是全0
*/
if (n<=0) return false;
return ((n&(n-1))==0);
划重点:
一个数*2,出相当于左移一位
2.判断是不是3的幂,没啥用的一道题
public boolean isPowerOfThree(int n) {
/*
不能用循环和递归 感觉不看答案是做不出来
3的幂都可以被int中最大的3的倍数整数
*/
return n > 0 && (1162261467 % n) == 0;
}
3.判断是不是4的幂
重点就是记住:0x55555555,8个5这个16进制的二进制是:奇数位1,偶数位0,用于提取出一个数的奇数位
/*
2的幂中有些不是4的幂,哪些呢?多写几个不难发现
2的幂特征是:最高位是1,后边的是0
而4的幂除了上边的特征,还有最高位必须是从后边数第奇数位
比如8的二进制是1000,从后边数第4位是最高位,不是4的幂,同理还有32
判断最高位是不是偶数位,用0x55555555&上就行,因为这个数的二进制表示
是奇数位为1,偶数位为0
*/
public boolean isPowerOfFour(int num) {
return num>0&(num&(num-1))==0&(num&0x55555555)!=0;
}
[LeetCode]231. Power of Two判断是不是2\3\4的幂的更多相关文章
- LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )
1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- 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是不是 ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- Java [Leetcode 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- (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 @ ...
随机推荐
- OllyDbg使用入门
OllyDbg的四个窗口: http://www.360doc.com/content/16/0913/07/16447955_590419156.shtml 反汇编窗口:显示被调试程序的反汇编代码, ...
- Spring Boot + Redis 初体验
本文测试环境: Spring Boot 2.1.4.RELEASE + Redis 5.0.4 + CentOS 7 让程序先 run 起来 安装及配置 Redis 参考: How To Instal ...
- 大厂是如何用DevCloud流水线实现自动化部署Web应用的?
DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design Ng组件库:ng-devui(欢 ...
- 区块链学习7:超级账本项目Hyperledger与Fabric以及二者的关系
☞ ░ 前往老猿Python博文目录 ░ 一.超级账本(hyperledger) 超级账本(hyperledger)是Linux基金会于2015年发起的推进区块链数字技术和交易验证的开源项目,成员包括 ...
- PyQt(Python+Qt)学习随笔:键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Qt中的焦点有键盘焦点和逻辑焦点(Logic Focus与Keyboard Focus )的区分,键 ...
- PyQt(Python+Qt)学习随笔:复选框checkBox的tristate属性
在Qt Designer中,tristate属性是复选框checkBox相比较于QAbstractButton多出来的唯一属性. tristate属性表示复选框是三种状态还是两种状态,如果trista ...
- CTF流量分析题大全(掘安攻防平台)
突然想做一下流量分析题,记得掘安攻防实验室上面有很多的流量分析题目,故做之 流量分析题一般使用的都是wireshark,(流量分析工具中的王牌 夺取阿富汗 说了分析http头,所以直接过滤http协议 ...
- Codeforces Edu Round 50 A-D
A. Function Height 由于只能提升\(x\)为奇数的点,每个三角形的底一定为\(2\), 则要求我们求: \(2 * (h_1 + h_2 + - + h_n) / 2 = k\),使 ...
- Golang之应用测试
Go 应用测试 测试的覆盖率 命令: go test ./ -v -cover 在<Go Web 编程>一书中,有以下结论: 这并不是绝对的,测试文件可以在不同的包,进行测试也是不会出现问 ...
- springmvc中使用文件上传功能
项目代码:https://github.com/PeiranZhang/springmvc-fileupload Servlet3.0之前使用文件上传功能 Servlet3.0之前需要使用common ...