给定范围 [m,n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含m, n两端点)。
例如,给定范围 [5,7],您应该返回 4。

详见:https://leetcode.com/problems/bitwise-and-of-numbers-range/description/

Java实现:

将m和n中的所有整数相与,得到的结果是m和n中的所有数的共同高位保留,除共同高位之外的其他位置零。那么关键问题就是如何找到这个共同的高位,其实并不难,m和n中所有数的共同高位也就是m和n的共同高位。

方法一:

class Solution {
public int rangeBitwiseAnd(int m, int n) {
int res=0;
while(m!=n){
m>>=1;
n>>=1;
++res;
}
return (m<<res);
}
}

方法二:

class Solution {
public int rangeBitwiseAnd(int m, int n) {
while (m < n){
n &= (n - 1);
}
return n;
}
}

C++实现:

class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int offset=0;
while(m!=n)
{
m>>=1;
n>>=1;
++offset;
}
return (m<<offset);
}
};

参考:https://www.cnblogs.com/grandyang/p/4431646.html

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. Leetcode201. Bitwise AND of Numbers Range数字范围按位与

    给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...

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

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

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

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

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

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

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

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

  7. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

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

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

  9. 201. Bitwise AND of Numbers Range -- 连续整数按位与的和

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

随机推荐

  1. request.getAttribute()与request.setAttribute()

    request.getAttribute()与request.setAttribute() request.getAttribute("nameOfObj")可得到JSP页面一表单 ...

  2. 手把手教你_怎么找android应用的包名和启动activity

    自己主动化測试中常常遇到这个问题,关于这个题目,方法众多,咱的目的是找个比較简单靠谱的: 方法一: 先进入cmd窗体,adb shell 后: cd /data/data ls 能够看到包名了吧,缺点 ...

  3. @Html.ValidationMessageFor客户端验证

    <%=Html.LabelFor(model => model.sUser) %><%=Html.TextBoxFor(model => model.sUser) %&g ...

  4. 代理ip proxy

    import requestsimport timefrom selenium import webdriverfrom selenium.webdriver.chrome.options impor ...

  5. “千千静听”滚动标题栏,非常简单!(时间器控制窗口标题栏文字,然后赋值给Application.Title)

    记得曾写过类似功能,但由于对Delphi数据类型不清楚,要花不少代码去处理中文被切半而出现乱码的尴尬.后来知道只需把字符串定义成 WideString 即可解决半个中文的问题了. 实现过程:不停地剪切 ...

  6. mipi屏在内核可以显示logo但是u-boot无法显示的问题【转】

    本文转载自:http://blog.csdn.net/fulinus/article/details/45071721 平台:瑞芯的rk3288 u-boot版本:u-boot-2014.10 ker ...

  7. Android控件之HorizontalScrollView 去掉滚动条

    在默认情况下,HorizontalScrollView控件里面的内容在滚动的情况下,会出现滚动条,为了去掉滚动条, 只需要在<HorizontalScrollView/>里面加一句 and ...

  8. [USACO 2017DEC] Barn Painting

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5141 [算法] 树形DP 时间复杂度 : O(N) [代码] #include< ...

  9. python编写九九乘法表代码

    打印九九乘法表 代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # 项目二: # 1.要求:编写九九乘法表 # 2.分析: # 根据九九乘法表的样式 ...

  10. bzoj4631

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 260  Solved: 133[Submit][Status][Discuss] ...