C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解
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
思路:
两个数按位与的结果为相同的部分保持不变,不相同的部分会被置为零,多个数的按位与的结果与之类似。
根据按位与的性质可知,如果数字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.
C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解的更多相关文章
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- Java实现 LeetCode 201 数字范围按位与
201. 数字范围按位与 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入 ...
- leetcode 201. 数字范围按位与 解题报告
给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 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 ...
- 区间数字的按位与 Bitwise AND of Numbers Range
2018-08-13 22:50:51 问题描述: 问题求解: 首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0. 之后需要将m 和 n将右移一位,直到m 和 n相等. 本质上, ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- 201 Bitwise AND of Numbers Range 数字范围按位与
给定范围 [m,n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含m, n两端点).例如,给定范围 [5,7],您应该返回 4. 详见 ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- C#版 - Leetcode 306. 累加数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- MyBatis sqlsession 简化 使用工具类创建
2019-04-09 @Test public void Test() throws Exception { // 1.读取配置文件 String resource = "mybatis-c ...
- 【转】Mac 删除文件夹里所有的.svn文件
转自: mac 删除文件夹里所有的.svn文件 想要把SVN專案作轉移或複製時 舊的「.svn」真的是很煩人的東西 最快的方式是用終端機輸入 sudo find /Users/justfly/Do ...
- Java IO 整理
1.Java IO中的操作类之间的继承关系 2.在java中使用File类表示文件本身,可以直接使用此类完成文件的各种操作,如创建.删除 3.RandomAccessFile类可以从指定位置开始读取数 ...
- docker遇到超时
1. 直接通过docker拉取镜像遇到的问题:熟悉的timeout!!! [root@localhost ~]# docker search mysql INDEX NAME DESCRIPTION ...
- vue实现二级联动效果
你如城市与省份间的二级联动效果 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- 查找datatable 中的重复记录(只查询一个字段)
StringBuilder str = new StringBuilder(); var res = new ResParameter() { code = ResponseCode.exceptio ...
- 关于height、offsetheight、clientheight、scrollheight、innerheight、outerheight的区别
二.也是平时经常用到的offsetheight 它返回的高度是内容高+padding+边框,但是注意哦,木有加margin哦,当然一般也木有啥需要把margin加进去的,以上代码为例,结果显示上图h2 ...
- Linux了解知识点
Linux知识点 1.linux系统内核最早由芬兰大学生linus Torvalds开发. 2.Linux主要用于服务器端和嵌入式两个领域. 3.Linux的特点:开放性.多用户.多任务.良好的用 ...
- 神经网络_线性神经网络 1 (Nerual Network_Linear Nerual Network 1)
2019-04-08 16:59:23 1 学习规则(Learning Rule) 1.1 赫布学习规则(Hebb Learning Rule) 1949年,Hebb提出了关于神经网络学习机理的“突触 ...
- [译文]Domain Driven Design Reference(六)—— 提炼战略设计
本书是Eric Evans对他自己写的<领域驱动设计-软件核心复杂性应对之道>的一本字典式的参考书,可用于快速查找<领域驱动设计>中的诸多概念及其简明解释. 其它本系列其它文章 ...