[LeetCode#201] Bitwise AND of Numbers Range
Problem:
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.
Analysis:
The idea behind this problem is not hard, you could easily think out the solution. But for the implementation, you may trap yourself into some sterotypes.
Idea:
Since for bitwise 'AND' operation, as long as there is a '0' appears, the digit should become 0. Principle in digit change in range: (work for numerial system, binary system and other system too)
Start: 1011111100111010101010
End: 1011111110001010010101 To reach 10001010010101 from 00111010101010. We must start to add
00000000000001 onto 00111010101010 => 00111010101011
...
We can we reach the '1' in high index, all digits behind after it must change once. That's to say, all the digits after 1 (include 1) should become 0.
answer: 1011111110000000000000 Algorithm:
Step 1: Find the first left bit 'start' differ from 'end'.
Start: 10111111[0]0111010101010
End: 10111111[1]0001010010101 Step 2: Make all bits after the first different bits (include first different bit) into 0. The number is the answer we wish to get.
10111111[0]0111010101010
=> 10111111000000000000 Great implementation:
Move bit to reach the answer!!!! See the solution!
Solution:
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
if (m < 0 || n < 0)
throw new IllegalArgumentException("the passed in arguements is not tin the valid range!");
int p = 0;
while (m != n) {
m = m >> 1;
n = n >> 1;
p++;
}
return m << p;
}
}
[LeetCode#201] Bitwise AND of Numbers Range的更多相关文章
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- 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 ...
- leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0
https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...
- 【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 <= ...
- 201. Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
随机推荐
- Hadoop 2.6.3动态增加/删除DataNode节点
假设集群操作系统均为:CentOS 6.7 x64 Hadoop版本为:2.6.3 一.动态增加DataNode 1.准备新的DataNode节点机器,配置SSH互信,可以直接复制已有DataNode ...
- 使用linq获得当前文件夹下的下一级满足条件的文件夹
使用linq获得当前文件夹下的下一级满足条件的文件夹. SPFolderCollection subAlbums = Folder.SubFolders; ...
- 怎样在win7上远程连接linux系统
window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤 在前面的环境和软件都有的情况下,双击putty.exe软件,如下图: 在软件界面中的:Hos ...
- XML&DTD&XML Schema学习
XML(eXtensible Markup Language)可扩展的标记语言.xml在web service编程中尤为重要.在网络传输中可以作为传输数据的载体.xml作为元语言,它可以用来标记数据. ...
- 按钮效果 css
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Gherkin学习笔记
前言 由于项目准备使用BDD模式开发,所以最近在学习BDD,同时也记录下自己的学习点滴. 参考原文:https://github.com/cucumber/cucumber/wiki/Gherkin ...
- C#程序中:如何向xml文件中写入数据和读取数据
xml文件作为外部信息存储文件使用简单,方便,其结构和表格略有相似,下面简单的说一下xml文件内容的读取 …… using System.Xml;using System.IO;namespace W ...
- 桂电在线_微信公众平台开发之-运用angularjs显示学校公告新闻列表和详情页面
折腾angularjs的感悟 几天折腾,总的来说看了很多博客,要么不是最新的技术文档,要么写得不够完整,因为别人是基于他们的理解写的技术博客代码不一定会贴完整,所以一旦你用的是最新的想要看完整的实例就 ...
- yii2源码学习笔记(十五)
这几天有点忙今天好些了,继续上次的module来吧 /** * Returns the directory that contains the controller classes according ...
- 利用def生成dll文件
DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被 ...