[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 ...
随机推荐
- wiki 使用笔记
Wiki 安装:Linux(Redhat EL5.3)下安装配置MediaWiki wiki配置: 配置文件:DefaultSettings.php //权限等配置 左边导航条:/wiki/inde ...
- Node.js + Express + Mongodb 开发搭建个人网站(二)
二.路由 1.打开 routes/index.js ,这个意思就是 捕获到访问主页的get请求: 并通过 app.js 分配到对应的路由里: 看到这里,打开 http://127.0.0.1:300 ...
- (转)教你如何使用php session
PHP session用法其实很简单它可以把用户提交的数据以全局变量形式保存在一个session中并且会生成一个唯一的session_id,这样就是为了多了不会产生混乱了,并且session中同一浏览 ...
- iOS开发内购图文教程
2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步.png 第二步.jpg ...
- CoreAnimation6-基于定时器的动画和性能调优
基于定时器的动画 定时帧 动画看起来是用来显示一段连续的运动过程,但实际上当在固定位置上展示像素的时候并不能做到这一点.一般来说这种显示都无法做到连续的移动,能做的仅仅是足够快地展示一系列静态图片,只 ...
- html-----002
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- winform程序中界面的跳转问题
首先是我们进行窗口间的跳转,尤其注意的是winform程序里面的空间都是中线程安全的.但是注意的是如果你在一个线程中操纵另外的控件,这时候会提示你一个错误,这个错误的解决方法准备单独的在另一篇文章中来 ...
- HTML5 程序设计笔记(一)
HTML5 概述 1.html5 发展史 1993年html首次以因特网草案形式发布. 20世纪90年代,html大幅发展,从2.0版,到3.2版和4.0版.最后到1999年的4.01版. 伴随htm ...
- Fedora 21 中添加及更新源的命令
原文: Fedora 21 中添加及更新源的命令 fedora的软件源信息文件(*.repo)都是放在 /etc/yum.repos.d 目录下的.可以通过# ls -l /etc/yum.repos ...
- laravel学习:修改时区
config/app.php 'timezone' => 'UTC', 改为 'timezone' => 'PRC',