【问题描述】[中等]

【解答思路】

1. 暴力

逐位与 ,只需要判断i= 0 或 i == 2147483647 的话,就跳出 for 循环即可。

时间复杂度:O(N) 空间复杂度:O(1)

public int rangeBitwiseAnd(int m, int n) {//m 要赋值给 i,所以提前判断一下if(m == Integer.MAX_VALUE){return m;}int res = m;for (int i = m + 1; i <= n; i++) {res &= i;if(res == 0 ||  i == Integer.MAX_VALUE){break;}}return res;
}
2. 位移


时间复杂度:O(logN) 空间复杂度:O(1)

class Solution {public int rangeBitwiseAnd(int m, int n) {int shift = 0;// 找到公共前缀while (m < n) {m >>= 1;n >>= 1;++shift;}return m << shift;}
}
3. Brian Kernighan


时间复杂度:O(logN) 空间复杂度:O(1)

class Solution {public int rangeBitwiseAnd(int m, int n) {while (m < n) {// 抹去最右边的 1n = n & (n - 1);}return n;}
}

【总结】

1. 位运算

异或运算(^)
运算规则:0^0=0; 0^1=1; 1^0=1; 1^1=0;
即:参加运算的两个对象,如果两个相应位为“异”(值不同),则该位结果为1,否则为0。
5 ^ 1 = 0101 ^ 0001 = 0100 = 4
5 ^ 3 = 0101 ^ 0011 = 0110 = 6
用法

  1. 翻转指定位 对应位异或1
    X=10101110,使X低4位翻转,用X ^0000 1111 = 1010 0001即可得到。
  2. 两个数是否相等 ==0
    5 ^ 5 = 0101 ^ 0101 = 0000 = 0

与运算(&)
运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;
即:两位同时为“1”,结果才为“1”,否则为0
5 & 1 = 0101 & 0001 = 0001 = 1
5 & 2 = 0101 & 0010 = 0000 = 0
用法
取指定位 对应位与1
设X=10101110,
取X的低4位,用 X & 0000 1111 = 00001110 即可得到;

或运算(|)
运算规则:0|0=0; 0|1=1; 1|0=1; 1|1=1;
即 :参加运算的两个对象只要有一个为1,其值为1
用法:
指定位置置1 对应位或1
将X=10100000的低4位置1 ,用X | 0000 1111 = 1010 1111即可得到

取反运算(~)
运算规则:~1=0; ~0=1;
即:对一个二进制数按位取反,即将0变1,1变0

带符号左移运算(<<)
若左移时舍弃的高位不包含1,则每左移一位,相当于该数乘以2(右边补0)
3 << 1 = 0011 <<1 = 0110 = 6
带符号右移运算(>>)
正数操作数每右移一位,相当于该数除以2
(左补0 or 补1得看被移数是正还是负)
5 >> 1 = 0101 >> 1 = 0010 = 2
5 >> 2 = 0101 >> 2 = 0001 = 1
-14 >> 2 = 11110010 >> 2 = 11111100 = -4
无符号右移运算(>>>)
5 >>> 1 = 0101 >>> 1 = 0010 = 2
-14 >>>2 =11111111 11111111 1111111111110010 >>>2 = 00111111 11111111 1111111111111100 = 1073741820
移位总结

  • 正数的左移与右移,负数的无符号右移,就是相应的补码移位所得,在高位补0即可。
  • 负数的右移,就是补码高位补1,然后按位取反加1即可。
2. 位运算 判相等换位异或^ 取位与&1 置位或|1
3. Brian Kernighan算法

n&(n-1) //n二进制最右位的 1 置 0

相关题目Brian Kernighan算法
[Leetcode][第461题][JAVA][汉明距离][位运算][Brian Kernighan]

参考链接:https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/solution/shu-zi-fan-wei-an-wei-yu-by-leetcode-solution/
参考链接:https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by–41/

[Leetcode][第201题][JAVA][数字范围按位与][位运算][Brian Kernighan]相关推荐

  1. [Leetcode][第461题][JAVA][汉明距离][位运算][Brian Kernighan]

    [问题描述][简单] [解答思路] 1. 内置位计数功能 时间复杂度:O(1) 空间复杂度:O(1) class Solution {public int hammingDistance(int x, ...

  2. [剑指offer]面试题第[65]题[JAVA][不用加减乘除做加法][位运算]

    [问题描述][简单] [解答思路] 1. 位运算 时间复杂度:O(1) 空间复杂度:O(1) public int add(int a, int b) {while(b != 0) { // 当进位为 ...

  3. leetcode 476. Number Complement | 476. 数字的补数(位运算)

    题目 https://leetcode.com/problems/number-complement/ 题解 class Solution {public int findComplement(int ...

  4. [Leetcode][第40题][JAVA][数组总和2][回溯][剪枝]

    [问题描述][中等] [解答思路] 1. 减法 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Ar ...

  5. [Leetcode][第17题][JAVA][电话号码的字母组合][回溯]

    [问题描述][中等] [解答思路] 用哈希表/数组存储每个数字对应的所有可能的字母,然后进行回溯操作. 回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是 ...

  6. [Leetcode][第889题][JAVA][根据前序和后序遍历构造二叉树][分治][递归]

    [问题描述][中等] [解答思路] copyOfRange class Solution {public TreeNode constructFromPrePost(int[] pre, int[] ...

  7. [Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]

    [问题描述][中等] [解答思路] public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) { ...

  8. [Leetcode][第78题][JAVA][子集][位运算][回溯]

    [问题描述][中等] [解答思路] 1. 位运算 复杂度 class Solution {List<Integer> t = new ArrayList<Integer>(); ...

  9. [Leetcode][第79题][JAVA][单词搜索][DFS][回溯]

    [问题描述][中等] [解答思路] 1. DFS繁琐版本 class Solution {public boolean exist(char[][] board, String word) {bool ...

最新文章

  1. powerDesign设计随笔
  2. golang sftp传输文件
  3. android Button背景高度被拉伸问题--解决方案
  4. 不同数据库中两列字段相减(某列有空值)
  5. php PDO 浮点数返回,php – 如何在PDO中简单地返回对象?
  6. android findviewbyid定义成静态,findViewById 为null???
  7. Linux系统查看版本和位数
  8. 【寻子】人脸识别与寻子的碰撞
  9. html select选择事件_用 Java 拿下 HTML 分分钟写个小爬虫
  10. 监控Nginx负载均衡器脚本
  11. Android Studio 解决 Cannot resolve symbol xxx 添加依赖后出现飘红
  12. spring springMvc spring-boot spring-cloud分别是什么
  13. 移动磁盘提示使用驱动器中的光盘之前需要格式化文件怎么找回
  14. 内是不是半包围结构_什么是结构化面试、无领导小组讨论、结构化小组面试?教你快速搞懂国考面试三大形式。...
  15. 凯恩帝1000对刀图解_KND数控车床对刀方法
  16. linux系统查看加密狗,[原创]linux下hasp(srm)加密狗的数据监控
  17. 计算机加减乘除的公式,excel公式汇总(excel公式加减乘除)
  18. Web安全攻防世界05 easyphp(江苏工匠杯)
  19. 从0到1构建基于Springboot+SpringCloud的微信点餐系统
  20. 英语翻译作业(十二)

热门文章

  1. 1.0 C++远征:数据的封装
  2. error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“MTd_StaticDebug”...
  3. javascript 获取上一页的url
  4. 【转载】快速、可伸缩和流式的AJAX代理--跨域持续内容分发
  5. Sublime Text 3 、WebStorm配置实时刷新
  6. gradle sync failed——Android studio 突然就无法自动下载gradle了
  7. Didn't find class cn.jpush.android.service.DownloadProvider on path:
  8. Android 贴纸样式标签
  9. 申请鲲鹏920测试机试水+编译nginx
  10. 我的虚拟化设想(My virtualization vision)