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的更多相关文章

  1. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  2. 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 ...

  3. 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 ...

  4. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0

    https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...

  5. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  6. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  7. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  8. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  9. 201. Bitwise AND of Numbers Range

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

随机推荐

  1. php 5.3起弃用session_register

    最近下了dedecms V5.7时,在登陆后台时,用户名和密码也没错,就是跳转不走,进不了后台管理页面,追踪了好久才发现根目录/include/userlogin.class.php中289行左右的位 ...

  2. springmvc使用@ResponseBody返回json乱码解决方法

    1.springmvc 3.2以上的版本解决乱码的方法: 第一步:在配置中加入: <mvc:annotation-driven> <mvc:message-converters re ...

  3. ADO.Net技术

    Connection对象 1.连接数据库 通过SqlConnection对象的State属性判断数据库的连接状态: public override ConnectionState State{ get ...

  4. HDU-1114(背包DP)

    Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...

  5. Gym 100187B-A Lot of Joy

    题意:给一个字符串,将每个字符分开放进两个口袋,每次从两个口袋分别拿出一个字符,如果相同则开心,问开心的次数期望是多少. 分析:数学期望题,然而这是我最不拿手的...最后答案是每个字符在字符串出现的次 ...

  6. 【转】[转]order by 1是什么意思?

    [转][转]order by 1是什么意思? ORDER BY 1 表示 所select 的字段按第一个字段排序 ORDER BY ASC应该没有这样写法,ORDER BY 后面不是字段就是数字, 可 ...

  7. Java-Android 之应用停止错误

    在Android在手机上运行的时候: 经常会出现应用程序停止: 一: 因为触发的方法里面没有传值View 对象,方法报错

  8. Session,ViewState用法

      基本理论: session值是保存在服务器内存上,那么,可以肯定,大量的使用session将导致服务器负担加重. 而viewstate由于只是将数据存入到页面隐藏控件里,不再占用服务器资源,因此, ...

  9. SQL-Server索引漫谈

    http://www.cnblogs.com/teroy/archive/2013/05/23/3070547.html

  10. 第4条:多用类型常量,少用#define预处理指令

    定义常量的几种方式: 1.#define ANIMATION_DURAION 0.3         //定义了一个动画时长的常量, 预处理过程会把碰到的所有ANIMATION_DURAION一律替换 ...