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

在线提交: https://leetcode.com/problems/bitwise-and-of-numbers-range/

题目描述


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

示例 1:

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

示例 2:

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

  • 题目难度:Medium

  • 通过次数:139

  • 提交次数:449

  • 贡献者:amrsaqr

  • 相关话题 位运算


思路:

两个数按位与的结果为相同的部分保持不变,不相同的部分会被置为零,多个数的按位与的结果与之类似。

根据按位与的性质可知,如果数字m != n,则在m, n范围内的数的最后一位必然同时存在1和0,因此最后一位的按位与&的结果必为0。而如果m=0,所有数按位与的结果必然为0。

举例而言:

例1. [9, 11],其范围内的数字的二进制表示依次为:

1 001

1 010

1 011

这些数按位相与后的结果为1 000。

例2.[20, 23],其范围内的数字的二进制表示依次为:

101 00

101 11

这些数按位相与后的结果为101 00。

例3.[4, 23],其范围内的数字的二进制表示依次为:

00100

10111

这些数按位相与后的结果为00000,因为高位没有共同字符串。

因此,m,n范围内的数的按位与的结果就是各个数的各位对齐后高位共同数字串末尾全补0的值。

已AC代码:

public class Solution
{
    public int RangeBitwiseAnd(int m, int n)
    {
        // 题中规定数据范围: m, n中较大值<=31s = 2147483647
        int shiftCount = 0;
        int common = 0;
        GetCommonDigits(m, n, ref common, ref shiftCount);
        var res = common << shiftCount;  // 末位补0

        return res;
    }

    public void GetCommonDigits(int a, int b, ref int common, ref int shiftCount)
    {
        shiftCount = 0;
        while (a != b)
        {
            a = a >> 1;
            b = b >> 1;
            shiftCount++;
        }
        common = a;
    }
}

当然也可使用递归解法:

public static int RangeBitwiseAnd(int m, int n)
{
    if (m < n)
        return (RangeBitwiseAnd(m >> 1, n >> 1) << 1);
    else return m;
}

Rank:

You are here! Your runtime beats 96.88% of csharp submissions.

Refrence:

https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56789/C-Solution-with-bit-shifting-(Beats-90)

C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解的更多相关文章

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

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

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

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

  3. leetcode 201. 数字范围按位与 解题报告

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

  4. [Swift]LeetCode201. 数字范围按位与 | Bitwise AND of Numbers Range

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

  5. 区间数字的按位与 Bitwise AND of Numbers Range

    2018-08-13 22:50:51 问题描述: 问题求解: 首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0. 之后需要将m 和 n将右移一位,直到m 和 n相等. 本质上, ...

  6. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

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

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

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

  8. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

  9. C#版 - Leetcode 306. 累加数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. 三、OpenStack创建域,项目,用户和角色,验证,创建客户端脚本

    一.Identity服务为每个OpenStack服务提供身份验证服务. 身份验证服务使用域,项目,用户和 角色的组合. 1.创建service 项目 # openstack project creat ...

  2. BUAA面向对象设计与构造——第一单元总结

    BUAA面向对象设计与构造——第一单元总结 第一阶段:只支持一元多项式的表达式求导 1. 程序结构 由于是第一次接触面向对象的编程,加之题目要求不算复杂,我在第一次作业中并没有很好利用面向对象的特点, ...

  3. C# 动态调用WebService 3

    using Microsoft.CSharp; using System; using System.CodeDom; using System.CodeDom.Compiler; using Sys ...

  4. js基础知识易错点(一)

    最近替另一个项目招人,要求基础知识好,随便问了一些基础题,发现了一些易错的点,总结一下. 1.判断一个空数组 var arr = []; 1)JSON.stringify(arr) == " ...

  5. 洛谷p3800:Power收集

    考虑朴素的dp: 对于每一行的每一个点 枚举能到的所有点(类似bzoj1648 比这题简单的dp) 期望时间复杂度O(NMT) 显然是超时做法 那么我们发现只有k个点对答案有贡献 考虑对每一个有权值的 ...

  6. [转]C# 使用Conditional特性而不是#if条件编译

    转自: http://www.cnblogs.com/xibei666/p/5495561.html 概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug ...

  7. JavaScript中实现小数点后保留2位

    在项目中有时候会遇到要求输入的数字是整数或者小数点后绑定2位小数,因此可以用.toFixed(2)方法 下面是关于toFixed()方法的demo: <input type="numb ...

  8. WinForm的.Designer.cs代码内抛反射异常

    今天在项目内一个Winform增加控件后,无法打开,抛如下异常. System.Reflection.TargetInvocationException: Exception has been thr ...

  9. [GIT] 更新.repo目录

    cd .repo/manifests/ git co -f git pull

  10. 2.Git配置和关联GitHub

    1.配置本地信息, 右键Git Bush Here git config –global user.name '账号名' ##回车 git config –global user.email 邮箱 # ...