Bitwise AND of Numbers Range LeetCode
来源:互联网 发布:appstore安装不了软件 编辑:程序博客网 时间:2024/06/04 23:04
二进制的序列有一定规律
1: 0001
2: 0010
3: 0011
4: 0100
5: 0101
6: 0110
7: 0111
8: 1000
9: 1001
10:1010
11:1011
12:1100
13:1101
14:1110
15:1111
从第0bit位(最低)起,'1'都一连续2^bitId次数出现,只要m~n不都在某一个连续的'1'区间,该bit位最后的结果必然是0
<pre name="code" class="cpp">class Solution {public: int rangeBitwiseAnd(int m, int n) { if (m==n) return m; unsigned int _m=m,_n=n; unsigned int num=n-m+1,ret=0,j,st; for (unsigned int i=2;i<=_n;i<<=1){ if (num>i||(_m&i)==0) continue; j=i-1; st=_m&j; if (i-st>=num){ ret^=i; } } return ret; }};
0 0
- 如何使用Android最新的RecyclerView取代ListView?
- CAML语法- Query写法
- Windows Server 2008 R2 Standard升级到EnterPrise
- Makefile语法基础介绍
- C#-实验3
- Bitwise AND of Numbers Range LeetCode
- 第三周项目-1
- 分页
- linux C程序中调用shell终端的命令
- xcode4的环境变量,Build Settings参数,workspace及联编设置
- 数据结构:题目(2)在二元树中找出和为某一值的所有路径(树)
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记25 Lifecycle生命周期
- 《Effective C++》重点摘要(四)
- List中toArray()的使用方法