201. Bitwise AND of Numbers Range -- 连续整数按位与的和
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
int rangeBitwiseAnd(int m, int n) { int mask = 0xffffffff; /* find out the same bits in left side*/ while (mask != ) { if ((m & mask) == (n & mask)) { break; } mask <<= ; } return m & mask; }
Idea:
1) we know when a number add one, some of the right bit changes from 0 to 1 or from 1 to 0
2) if a bit is 0, then AND will cause this bit to 0 eventually.
So, we can just simply check how many left bits are same for m and n.
for example:
5 is 101
6 is 110
when 5 adds 1, then the right two bits are changed. the result is 100
6 is 110
7 is 111
when 6 adds 1, then the right one bit is changed. the result is 110.
9 is 1001
10 is 1010
11 is 1011
12 is 1100
Comparing from 9 to 12, we can see the first left bit is same, that's result.
201. Bitwise AND of Numbers Range -- 连续整数按位与的和的更多相关文章
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- Java for LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 201. Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
- [LeetCode#201] Bitwise AND of Numbers Range
Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- 201. Bitwise AND of Numbers Range (Bit)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND(按位与) of all nu ...
随机推荐
- MySQL(三) —— 约束以及修改数据表
约束: 1. 约束保证数据的完整性和一致性: 2. 约束分为表级约束和列级约束: 3. 约束类型包括:NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREI ...
- [CF738D]Sea Battle(贪心)
题目链接:http://codeforces.com/contest/738/problem/D 题意:1*n的格子里有a条长为b的船.有一个人射了k发子弹都没打中船,现在问最少再打多少次一定能保证射 ...
- 对MSP430单片机__delay_cycles精确延时的说明及改正
在这里, 我来讨论一下关于MSP430单片机使用__delay_cycles延时的问题. IAR for MSP430编译器提供了一个编译器内联的精确延时函数(并非真正的函数)以提供用户精确延时使用, ...
- ettercap
作者: 官网:http://ettercap.github.io/ettercap/ 源码:https://github.com/Ettercap/ettercap 功能:arp欺骗
- json、javaBean、xml互转的几种工具介绍 (转载)
工作中经常要用到Json.JavaBean.Xml之间的相互转换,用到了很多种方式,这里做下总结,以供参考. 现在主流的转换工具有json-lib.jackson.fastjson等,我为大家一一做简 ...
- Send to Kindle :一键推送网页内容到多看
http://site.douban.com/129629/widget/notes/7074800/note/207072907/ 注意:增加配置信息,一键发送,方便及时分享网页.
- 原生js如何获取当前所加载网页的文件路径和名称
结合使用string对象中的substr()和lastIndexOf()方法. 当前页面路径:file:///C:/Users/Administrator/Desktop/test.html < ...
- SAS Annotated Output GLM
SAS Annotated Output GLM 在使用SAS过程中,proc glm步输出离差平方和有4种算法,分别是SS1 SS2 SS3 SS4 下面文章介绍了其中SS3的具体计算步骤和例子 ...
- [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)
本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...
- [转载] 一致性问题和Raft一致性算法
原文: http://daizuozhuo.github.io/consensus-algorithm/ raft 协议确实比 paxos 协议好懂太多了. 一致性问题 一致性算法是用来解决一致性问题 ...