老程序员分享:leetcode笔记201.BitwiseANDofNumbersRange

简介: 老程序员分享:leetcode笔记201.BitwiseANDofNumbersRange

"

public int rangeBitwiseAnd(int m, int n) {

while(m

//代码效果参考:https://v.youku.com/v_show/id_XNjQwMDEyOTc4MA==.html

return n;

}

The key point: reduce n by removing the rightest '1' bit until n<=m;

(1)if n>m,suppose m = yyyzzz, n = xxx100, because m is less than n, m can be equal to three cases:

(a) xxx011 (if yyy==xxx)

(b) less than xxx011 (if yyy==xxx)

(c) yyyzzz (if yyy

for case (a), and (b), xxx011 will always be ANDed to the result, which results in xxx011 & xxx100 = uuu000(uuu == yyy&xxx == xxx);

for case (c), xxx000/xxx011 will always be ANDed to the result, which results in yyyzzz & xxx000 & xxx011 & xxx100 = uuu000 (uuu <= yyy & xxx)

=> for any case, you will notice that: rangBitWiseAnd(vvvzzz,xxx100) == uuu000 == rangBitWiseAnd(vvvzzz,xxx000), (not matter what the value of""uuu"" will be, the last three digits will be all zero)

=> This is why the rightest '1' bit can be removed by : n = n & (n-1);

(2)when n==//代码效果参考:https://v.youku.com/v_show/id_XNjQwMDEyNTU0MA==.html

m, obviously n is the result.

(3)when n < m, suppose we reduce n from rangBitWiseAnd(yyyzzz,xxx100) to rangBitWiseAnd(yyyzzz,xxx000);

i) xxx100 >yyyzzz => xxx >= yyy;

ii) xxx000 xxx <= yyy;

=> xxx == yyy;

=> rangBitWiseAnd(yyyzzz,xxx000) == rangeBitWiseAnd(xxxzzz,xxx000);

=>result is xxx000, which is also n;

转自 charlie.chen 的回答

通过不断削减最右的位数直到n的值小于等于m,此时就能得到想要的值

似乎是默认了返回值一定是小于等于m的一个数,所以对n进行按位减少直到小于等于m

当n>m时可以证明rangBitWiseAnd(yyyzzz,xxx100) 与 rangBitWiseAnd(yyyzzz,xxx000) 结果一定相同,从而削减n

对比自己的拙劣代码:

class Solution {

public:

int rangeBitwiseAnd(int m, int n) {

if(m == 0){

return 0;

}

int p = 1;

int ret = 0;

for (int i = 2; i/2 0;) {

if (m%i >= i/2 && n%i <= i - 1 && n % i >= m % i && n-m <= i/2-1)

ret += i / 2;

i = i * 2;

if (i < 0)

if (m% 2147483648 >= 1073741824 && n% 2147483648 <= 2147483647 && n % 2147483648 >= m % 2147483648 && n - m <= 1073741824 - 1)

ret += 1073741824;

}

return ret;

}

};

owensy的文章中对此也有相关分析

但实际上复杂度似乎并没有降低多少?甚至有提高?


"
image.png
相关文章
|
2天前
|
程序员 Go
程序员必知:【LeetCode】PourWater倒水
程序员必知:【LeetCode】PourWater倒水
|
9月前
|
算法
炸了!力扣官方首发了这套1568页LeetCode算法刷题笔记(彩页版)
你知道现在LeetCode算法在大厂中的重要性吗? 前几天小编看了一个国内算法大神的短视频,他就在视频中指出了算法对当下无论是生活还是找工作中都是非常重要的! 没错这个人就是江湖人称“左神”的左程云老师 小编也简单看了一下一些比较知名互联网大厂的招聘,像阿里,字节,美团,京东,百度等都在简介明确写上了要求“算法精通”! 那么如何达到“算法精通”今天小编特意给大家分享出一套1568页的LeetCode算法刷题(彩页版)笔记,助力你早日在简历写上“算法精通”
炸了!力扣官方首发了这套1568页LeetCode算法刷题笔记(彩页版)
|
1月前
|
存储 索引 Python
LeetCode刷题笔记(1)—— 两数之和
LeetCode刷题笔记(1)—— 两数之和
|
1月前
|
算法 Java 程序员
太全了!字节总监总结240道算法LeetCode刷题笔记
常言道「算法才是编程的灵魂」,不管是Java, python,还是PHP,都跨不过算法这个门槛。
|
8月前
|
存储 算法 Java
刷爆Leetcode!字节算法大佬进阶专属算法笔记:GitHub标星97k+
数据结构就是指一组数据的存储结构。算法就是操作数据的一组方法。 数据结构和算法是相辅相成的。数据结构是为算法服务的,算法作用在特定的数据结构之上。 因此,我们无法孤立数据结构来讲算法,也无法孤立算法来讲数据结构。
|
9月前
|
SQL 索引 Python
【笔记】LeetCode计划——30天Pandas挑战(4)
LeetCode1148题——文章浏览|
90 1
|
9月前
|
索引 Python
【笔记】LeetCode计划——30天Pandas挑战(3)
LeetCode183题——从不订购的客户
113 1
|
9月前
|
Python
【笔记】LeetCode计划——30天Pandas挑战(2)
LeetCode1757题——可回收且低脂的产品
73 1
|
9月前
|
SQL Python
【笔记】LeetCode计划——30天Pandas挑战(5)
LeetCode1683题——无效的推文
55 0
|
9月前
|
Python
【笔记】LeetCode计划——30天Pandas挑战(1)
LeetCode595题——大的国家 记录自己的学习心得以及解题时学到的知识和要注意的点
127 0