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

面试官,你再问我 Bit Operation 试试?

描述

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

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

如:

输入: [26,30]
输出: 24

解析

首先,将 [ 26 , 30 ] 的范围数字用二进制表示出来:

11010  11011  11100  11101  11110

而输出 24 的二进制是 11000 。

可以发现,只要找到二进制的 左边公共部分 即可。

所以,可以每次向左移一位,比较 m 和 n 是否相同,不同再继续左移一位,直至相同,然后相同的数再左移对应位数即可。

代码

public int rangeBitwiseAnd(int m, int n) {
int i = 0; // i means we have how many bits are 0 on the right
while(m != n) {
m >>= 1;
n >>= 1;
i++;
}
return m << i;
}

另一种:

可以先建立一个 32 位都是 1 的 mask,然后每次向左移一位,比较 m 和 n 是否相同,不同再继续左移一位,直至相同,然后把 m 和 mask 相与就是最终结果。

public int rangeBitwiseAnd(int m, int n) {
int d = Integer.MAX_VALUE;
while ((m & d) != (n & d)) {
d <<= 1;
}
return m & d;
}

[LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)的更多相关文章

  1. 201 Bitwise AND of Numbers Range 数字范围按位与

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

  2. Leetcode201. Bitwise AND of Numbers Range数字范围按位与

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

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

  4. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. _pvp_killed_loot

    该表控制玩家被击杀时掉落物品,包括角色身上装备,背包物品,银行物品 comment 备注 entry 掉落的物品ID lootCount 掉落的物品数量 chance 掉落的几率,例如50,则50%几 ...

  2. GYM 101064 2016 USP Try-outs G. The Declaration of Independence 主席树

    G. The Declaration of Independence time limit per test 1 second memory limit per test 256 megabytes ...

  3. 在JAVA中返回类型使用泛型T和Object有什么区别?

    最近在读jackson源码的时候发现有段代码返回类型写的是<T> T,而我自己一般写的是Object.上网搜了下这个语法糖,在stackoverflow上找到一个比较简单易懂的解释,搬运过 ...

  4. tomcat热部署.class

    本人是在维护公司系统时遇到的问题,由于公司的系统是部署到客户服务器上,而系统中存在的问题又比较多,需要经常维护.如果每次修改完class文件后都需要去重启服务器, 那会给用户的使用造成不便,所以需要使 ...

  5. OpenModelica读取文件

    parameter String file = Modelica.Utilities.Files.loadResource("J:/git/tcs/tcs.txt"); 将文件名变 ...

  6. vscode所用插件

  7. 第 8 章 容器网络 - 055 - 创建 macvlan 网络

    1.创建 macvlan 网络 在 host1 和 host2 中创建 macvlan 网络 mac_net1: docker network create -d macvlan --subnet=1 ...

  8. 记录python接口自动化测试--把测试结果写进excel文件(第九目)

    python中一般使用xlrd(excel read)来读取Excel文件,使用xlwt(excel write)来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用xlrd读取 ...

  9. centos 安装npm node

    最近那vue全套造了个管理系统的轮子,发现node简直太好用了. elment-UI的出现就是不懂ui设计的后台工程师的福音~ 正好自己买的两个云服务器空闲着没用,就拿来试试看了 首先软件都安装在/u ...

  10. 数据结构(C语言版)-第6章 图

    6.1 图的定义和基本术语 图:Graph=(V,E)  V:顶点(数据元素)的有穷非空集合:  E:边的有穷集合. 无向图:每条边都是无方向的 有向图:每条边都是有方向的 完全图:任意两个点都有一条 ...