Leetcode Power of Two
Given an integer, write a function to determine if it is a power of two.
题目意思:
给定一个整数,判断是否是2的幂
解题思路:
如果一个整数是2的幂,则二进制最高位为1,减去1后则最高位变为0,后面全部是1,相与判读是否为零,注意负数和0,负数的最高位是1。
更多二进制的问题可以参考《编程之美》中二进制有多少个1,面试时容易问。
源代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
return (n > ) && (n&(n-)==);
}
};
Leetcode Power of Two的更多相关文章
- [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, three, four
Given an integer, write a function to determine if it is a power of two. Hint: Could you solve it in ...
- 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. 题意:判断一个数是否是 ...
随机推荐
- MVC路由配置例
public static void RegisterRoutes(RouteCollection routes) { string suffix = string.Empty; routes.Ign ...
- required - HTML5里的input标签的required属性提示文字修改
input 里面增加这样的语句: <input type="text" placeholder="您的姓名" required oninvalid=&qu ...
- flask源码分析
本flask源码分析不间断更新 而且我分析的源码全是我个人觉得是很beautiful的 1 flask-login 1.1 flask.ext.login.login_required(func),下 ...
- python3 黑板客爬虫闯关游戏(四)
这关较第三关难度增加许多,主要多了并发编程 密码一共有100位,分布在13页,每页打开的时间在15秒左右,所以理所当然的想到要用并发,但是后来发现同IP访问间隔时间不能小于8秒,不然会返回404,所以 ...
- JetBrains PyCharm 2016.2.3注册码
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- Express的搭建--nodejs的学习
1,需要的学习环境 Node.js: 0.10.32 Express: 4.10.2 MongoDB: 2.6.1 2,安装Express $ npm install -g express-gener ...
- bootstrap-select js jQuery控制select属性变化
bootstrap-select我想大家都不陌生是一个前端下拉框的插件非常好用,在select的标签中设置属性可以做很多功能控制,不过初始化之后怎么去修改网上找遍中文英文也没有一个交代自己研究好久研究 ...
- IOS-UIDynamic
UIDynamic中的三个重要概念 Dynamic Animator:动画者,为动力学元素提供物理学相关的能力及动画,同时为这些元素提供相关的上下文,是动力学元素与底层iOS物理引擎之间的中介,将Be ...
- in (1,2)and in('1,2')解决
select wm_concat(org_name) from mstorg where instr((select pass_dists from licrequests where req_no= ...
- JDBC、DAO
JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力 JDBC的工作原理 JDBC 驱动器由数据库厂商提供 1.在个人开发与测试中,可以使用JDBC-ODBC桥连方式 2.在生产型开发 ...