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 ...
随机推荐
- Pycharm相对路径
问题: 今天有个程序,明显路径是存在的,但是os.path.exists的返回结果是False. 仔细想了想, 是相对路径的问题. 情况描述: 我的路径是: dir_path = 'data/mark ...
- 基于Emgucv,C#的图片旋转方式
/// <summary> /// 图片旋转 --百度 旋转仿射 /// </summary> /// <param name="modelImage" ...
- dotnet core 3.0 linux 部署小贴士
dotnet core 3.0 目前还是测试版,在linux下安装 sdk 需要有一些注意事项 1.下载url https://dotnet.microsoft.com/download/thank- ...
- 七牛云音频转码准备工作之如何创建音视频处理私有队列pipeline
如何创建音视频处理私有队列 最近更新时间:2017-08-28 15:54:45 在七牛进行音视频处理,推荐使用私有队列(pipeline). 创建私有队列方法如下: 第一步 登录七牛开发者平台 ht ...
- Python算术运算
一.算术运算1.四则运算1+(100-20)/4+5*22.乘方运算2**103.求摸运算7%5 4.取整运算 7//5 = 1 5.绝对值函数 abs(-100) 6.导入数学函数后才能执行类似 ...
- C#线程--5.0之前时代(一)--- 原理和基本使用
一.开篇概念明晰: 多任务: 协作式多任务:cpu可以处理多种任务,但是这些任务是排队等候的,当cpu在处理一个任务的时候,其他的任务被锁定,只有当cpu处理完当前任务,才可以继续处理下一个任务(专一 ...
- web 11
Obtaining the JSON: 1.首先,我们将把要检索的JSON的URL存储在变量中. 2.要创建请求,我们需要使用new关键字从XMLHttpRequest构造函数创建一个新的请求对象实例 ...
- 实现lodash.get功能
function deepGet(object, path, defaultValue) { return (!Array.isArray(path) ? path.replace(/\[/g, '. ...
- [LeetCode] Bus Routes 公交线路
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...
- 【原创】.Net WebForm Calendar 日历控件常用方法
微软官方地址 https://msdn.microsoft.com/en-us/library/add3s294.aspx 1.设置日历控件单个日期Table Cell样式 颜色/外观/边距 prot ...