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 ...
随机推荐
- Using SYSTEM.MOUSE_ITEM In Oracle Forms
If the mouse is in an item, SYSTEM.MOUSE_ITEM represents the name of that item as a CHAR value.For e ...
- sql默认启动密码
首先启动sqlplus输入用户名:sqlplus / as sysdba密码空缺如果用户被锁定,记得加上SQL> alter user dbsnmp account unlock; User a ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- win8和ubuntu双系统安装
做了一个windows和Ubuntu双系统,参考了一些文章.网上资料不少,我就不重复了. 虽然没什么难度,但是有些细节在装的时候需要注意.不然造成资料丢失,系统崩溃,你就得不偿失,需要折腾花费较长的时 ...
- 纯js写验证码
<html> <head> <meta name="viewport" content="width=device-width" ...
- ubuntu 安装遇到黑屏
1.安装了ubuntu之后,进入登录页面,然后用输入密码后就黑屏了,按ctrl+alt+f1都没用,也进不去命令界面,查找资料后发现是必须在虚拟机->设置->硬件->显示器,上把加速 ...
- iOS - UIKit
1.UIKit 框架基本结构 1)控件 屏幕上的所有 UI 元素都叫做控件(也有叫做视图.组件),比如按钮(UIButton).文本(UILabel)都是控件. 为了便于开发者打造各式各样的优秀 Ap ...
- 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD
上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...
- hdu 4414 Finding crosses
题目链接:hdu 4414 其实是一道简单的字符型水题,不涉及任何算法,可比赛时却没能做出来,这几天的状态都差到家了... 题目大意是求有多少个满足条件的十字架,十字架的边不能有分叉路口,所以枚举每个 ...
- openfire过滤脏话插件,控制消息是否发送
参考:http://myopenfire.com/article/getarticle/9 package com.myopenfire.plugin; import java.io.File; im ...