算法系列——Power of Two
来源:互联网 发布:新闻资讯网站php源码 编辑:程序博客网 时间:2024/06/03 14:18
题目描述
Given an integer, write a function to determine if it is a power of two.
解题思路
还是考察位操作。如果一个数n是2的幂,例如 4( 100)
4-1=3(011) ,减去一后会将原来高位1 后面的位全部补为1,4(100)&3(011)=0 ,进行与操作,结果为0.
程序实现
public class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; return ((n-1)&n)==0; }}
阅读全文
0 0
- 算法系列——Power of Two
- 算法细节系列(2):231.Power of Two && Three
- LeetCode231——Power of Two
- Leetcode——231. Power of Two
- Algorithms—231.Power of Two
- leetcode系列(13)Power of two
- 算法系列——Intersection of Two Arrays
- 算法系列——Intersection of Two Arrays II
- 算法系列——Intersection of Two Linked Lists
- 15算法课程 231. Power of Two
- leetcode——231—— Power of Two
- leetcode解题报告233——Power of Two
- leetcode刷题日记——Power of Two
- LeetCode每日一题——231. Power of Two
- Power of Two
- [LeetCode] Power of Two
- 【LeetCode】Power of Two
- Power of Two
- jmeter中文版新手入门教程
- MySQL模糊查询特殊字符如何查询?
- 浮动和清除浮动
- Windows+Anaconda2环境下安装测试basemap
- Linux Centos 可以平通IP和域名,但打开火狐却上不了网
- 算法系列——Power of Two
- 锚伪类-关于"<a>"链接状态
- 解决 -- java 调用webservice 服务端收到参数为null
- CSS Hack详解 支持IE 6-11、Chrome、FireFox、Safari、Opera
- HDU 1242 Rescue (DFS+剪枝,一个起点多个终点)
- VS2013 配置使用微软开源sdk: C++ REST SDK 及运行官方的 JSON例子
- ArcGIS Engine 添加SDE数据库
- bzoj 3597: [Scoi2014]方伯伯运椰子 spfa判负环+分数规划
- 搭建本地服务器的方法: