[leetcode] Bitwise AND of Numbers Range(位运算)

不得不说位运算的应用都是很巧妙的。

这道题求一个连续区间整数的与运算的值,突破点在于连续的区间这个特点。我们可以先找几个数看一下规律,

2 3 4 5的二进制分别是 10 ,11,100,101,可以发现若m==n,则m为答案;当m!=n时,因为连续的两个数的二进制

的最后一位肯定不一样,与的值一定是0,前面相同的部分(1&1=1,0&0=0)保持。

所以我们每次先判断,不同的话就右移一位,比较前面的,直到相同的时候结束,最后左移  移动的位数。

不必担心左移的时候有1会导致溢出,从而失败,因为右移的时候前面已经补0了。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-05-22 08:07:19

[leetcode] Bitwise AND of Numbers Range(位运算)的相关文章

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 in this range, inclusive. For example, given the range [5, 7], you should return 4. 题解:如果m==n,那么答案就是m. 如果m<n,那么二进制最右边一位在最后的结果中肯定是0,那么就可以转化成子问题: rangeBi

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

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for adding this problem and creatin

Leetcode Bitwise AND of Numbers Range

题目地址:https://leetcode.com/problems/bitwise-and-of-numbers-range/ 题目解析:参考https://leetcode.com/problems/number-of-1-bits/,我们知道n&n-1为n和n-1两者左边相同的部分,那么容易得到n-1&n-2为n-2与n-1左边相同部分,依次类推,m&m+1....&n为m与n左边相同部分. 题目解答: public class Solution { public i

【LeetCode】201. Bitwise AND of Numbers Range

Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits:Special thanks to @amrsaqr for a

leetcode_201题——Bitwise AND of Numbers Range(位操作)

Bitwise AND of Numbers Range Total Accepted: 9698 Total Submissions: 35771My Submissions Question Solution Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given th

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so t

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

在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 题意:

codeforces 165E - Compatible Numbers 【位运算】

题目:codeforces 165E - Compatible Numbers 题意:给出n个数,然后每个数对应输出一个当前数组中与 Ai 与运算为 0 的数,没有的话输出-1 分析:简单的位运算题目,技巧性题目 首先,我们知道与运算的性质,就是只有同时为 1的时候才是1,那么假如 x&y=0 ,就是至少 x 的为1的为 y 全为0,其他为自由,假设为 1 ,那么 y = x^((1<<22)-1).但是这样并不是全部的,这些位还可能是0,所以我们可以枚举这些位,然后处理. 具体看代码

LeetCode刷题总结-双指针、位运算和分治法篇

本文总结LeetCode上有关双指针.位运算和分治法的算法题,推荐刷题总数14道.具体考点分析如下图: 一.双指针 1.字符串和数组问题 题号:424. 替换后的最长重复字符,难度中等 题号:828. 独特字符串,难度困难 题号:923. 三数之和的多种可能,难度中等 2.实际场景应用问题 题号:826. 安排工作以达到最大收益,难度中等 3.元素对问题 题号:986. 区间列表的交集,难度中等 二.位运算 1.字符串和数组问题 题号:137. 只出现一次的数字 II,难度中等 题号:318.