LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0
https://leetcode.com/problems/bitwise-and-of-numbers-range/
[n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值
比如
5:101
7:111
结果就是
4:100
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int len = 0;
long long tmp = 1;
for(;tmp <= n || tmp <= m;len++){tmp<<=1;}
int ans = 0;
for(int i = len - 1;i>=0;i--){
if(((1<<i) & n) == ((1<<i) & m))ans |= (1<<i)&m;
else break;
}
return ans;
}
};
LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0的更多相关文章
- 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 ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- Java for LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode#201] Bitwise AND of Numbers Range
Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- 201. Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...
随机推荐
- VBS整人代码
记得刚开始学VB脚本语言的时候,写了一段调用系统进程的代码,挺好的: dim wshif msgbox("笑笑很帅",vbyesno,"请回答是或否")=vby ...
- 一个简单的loading,纯属自娱自乐
/// <reference path="/scripts/js/jquery.min.js" /> var zsw = { loading: function (im ...
- 理解 JavaScript 作用域和作用域链
http://www.cnblogs.com/lhb25/archive/2011/09/06/javascript-scope-chain.html
- Mifare系列6-射频卡与读写器的通信(转)
文/闫鑫原创转载请注明出处http://blog.csdn.net/yxstars/article/details/38085415 1. 复位应答(Answer to request) 读写器呼叫磁 ...
- HttpClient 4.5.x 工具类设计与实现
最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ...
- 在ionic/cordova中使用Form模型验证(w5cValidator)
在构建ionic项目过程中,当我们创建一个类似表单提交的页面时,可能会对用户的输入内容做某些规则验证,通过后再执行提交处理. 在验证的过程中,为了提供较好的用户体验,可能希望有类似于jquery Va ...
- Web墨卡托投影(转)
Google Maps地图投影全解析(1):Web墨卡托投影 Google Maps.Virtual Earth等网络地理所使用的地图投影,常被称作Web Mercator或Spherical Mer ...
- 数据库多表连接方式介绍-HASH-JOIN
1.概述 hash join是一种数据库在进行多表连接时的处理算法,对于多表连接还有两种比较常用的方式:sort merge-join 和 nested loop. 为了比较清楚的介绍hash joi ...
- VIM小技巧
1.复制多行 vi编辑器中的整行(多行)复制与粘贴就非常必要了. 1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按"yy"进行复制: 2)多行复制 在命令模式下 ...
- Kafka是如何实现高吞吐率的
Kafka是如何实现高吞吐率的 原创 2016-02-27 杜亦舒 性能与架构 Kafka是分布式消息系统,需要处理海量的消息,Kafka的设计是把所有的消息都写入速度低容量大的硬盘,以此来换取更强的 ...