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 ...
随机推荐
- 怎么给easyui中的datagrid加水平滚动条
注意如下几个点就行: 1.数据网格(DataGrid)所在的table属性上级div无需设置width: 2..datagrid属性:fitColumns为false 或者不填 3.在style中给. ...
- 动态规划——Remove Boxes
很久没写博客了,越来越懒了,这次还是要分享LeetCode上一道动态规划的题目,和之前的Ballon Boom那个题(我记得是这个标题吧...)差不多,都是对一个数组的区间进行枚举的题,而且涉及到区间 ...
- Oracle 与 Mysql NULL值,空字符串''的区别
Oracle(null等同于空字符'') 1.oracle插入空字符串默认替换成null 2.oracle查询(null和被替换的空字符)时使用 is null/is not null 3.使用聚合函 ...
- BZOJ2143: 飞飞侠
2143: 飞飞侠 题意: 给出两个 n ∗ m 的矩阵 A,B,以及 3 个人的坐标 在 (i, j) 支付 Ai,j 的费用可以弹射到曼哈顿距离不超过 Bi,j 的位置 问三个人汇合所需要的最小总 ...
- Android应用程序支持不同屏幕(尺寸、密度)
how to build a user interface using Android layouts for all types of devices 使用Android布局设计的UI接口用于不同的 ...
- js 模拟form表单post提交
var generateHideElement = function (name, value) { var tempInput = document.createElement("inpu ...
- jquery获取一组文本框的值
$("#btn5").click(function() { var str="";$("[name='checkbox'][checked]& ...
- python入门 -- 环境搭建(windows)
1. 下载Anaconda Anaconda内置了python解释器及经常使用的库,提供了编译好的环境.根据自己的操作系统,自行从下面网站挑选一个较新的版本,下载安装即可. https://mirro ...
- 201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
实验十二 图形程序设计 实验时间 2018-11-14 1.实验目的与要求 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: 创建空框架 . 在Java中,常采用框架(Frame) ...
- TP框架下载功能
namespace Home\Controller; use Think\Controller; use Org\Net\Http; class IndexController extends Con ...