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

示例 1:

输入: [5,7]
输出: 4

示例 2:

输入: [0,1]
输出: 0

思路分析

由于是按位与,那么某位一旦出现0,结果该位肯定是0。所以只需要考虑m,n都是1的位置。那么直接从高位开始,往低位走,直到遇到该为的数字不相等,将其后的数为都置为0,即为[m,n]之间所有的数字按位与的结果。代码如下

#include<bits/stdc++.h>

using namespace std;
static auto x = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
return 0;
}(); class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int ret = 0;
for(int i = 31; i >= 0; --i){
int c = (1<<i);
if((m&c) == (n&c)){
ret |= (m&c);
}else break;
}
return ret;
}
}; int main() { return 0;
}

leetcode 201. 数字范围按位与 解题报告的更多相关文章

  1. C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解

    C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...

  2. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

  3. Java实现 LeetCode 201 数字范围按位与

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

  4. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  5. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  6. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  8. 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)

    [LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  9. 【LeetCode】851. Loud and Rich 解题报告(Python)

    [LeetCode]851. Loud and Rich 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

随机推荐

  1. 关于Linux部分版本无法安装Chrome的问题

    在想要yum安装Chrome浏览器后发现安装没有相应的包,在查询后得知Chrome已经对Redhat和Centos等部分版本停止支持, 所以这些新版的系统中直接安装就显得有些困难了,那么从网上找到了一 ...

  2. git 提交 src refspec master does not match any

    git init 产生的目录解释   error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error ...

  3. path、classpath理解

    path.classpath最常见的场景:环境变量配置 path环境变量:设置path的作用是让操作系统可以找到JDK命令(指定了JDK命令搜索路径):path环境变量原来Windows里面就有,只需 ...

  4. JS isArray、typeof、instanceof

    Array.isArray() 用来检验是不是数组 var a = [1,2,3] console.log(typeof a); // object console.log(Array.isArray ...

  5. 关于js的严格模式

    最近在看你不知道js,补充自己的js基础,加深理解.在读的过程中写点笔记. 严格模式下与非严格模式的区别 . 严格模式是es5新增的,es6是默认为严格模式的!js默认状态下是非严格模式的!   一般 ...

  6. poj_3696_The Luckiest number

    Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...

  7. Linux自带mariadb卸载

    MySQL安装过程中报错: dpkg: regarding mysql-community-server_5.6.39-1debian9_i386.deb containing mysql-commu ...

  8. linux 安装 zookeeper

    第一步 下载 zookeeper: http://archive.apache.org/dist/zookeeper/ 第二步 解压: tar -xzvf zookeeper-3.4.5.tar.gz ...

  9. 一个优秀的SSH远程终端工具

    SSH远程终端工具是一款在Windows界面下用来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的.向我们操控集群的时候,如果每台机器都安装一个显示器和键盘也是一个不小的花费,而远程终端 ...

  10. 史上最强大的wordpress后台框架redux-framework安装及使用

    redux-framework的相关链接 Redux的官方网站:https://reduxframework.com/ Redux文档查询:https://docs.reduxframework.co ...