【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
思路:
先找前面二进制相同的位,后面不相同的位相与一定为0.
比如: 1111001
1110011
从0011 - 1001 中间一定会经过 1000 从而使这四位都为0
我的代码:
int rangeBitwiseAnd(int m, int n) {
int ans = ;
int t = ;
while(t >= && ((m & ( << t)) == (n & ( << t))))
{
ans |= m & ( << t);
t--;
}
return ans;
}
别人的代码:更精简
int rangeBitwiseAnd(int m, int n) {
int r=Integer.MAX_VALUE;
while((m&r)!=(n&r)) r=r<<;
return n&r;
}
【leetcode】Bitwise AND of Numbers Range(middle)的更多相关文章
- 【leetcode】House Robber & House Robber II(middle)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 【leetcode】Binary Tree Right Side View(middle)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- Git工作流总结
引用自:https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md 说明: 个人在学习Git工作流的过程中,从原有的 S ...
- [译]ASP.NET 5: New configuration files and containers
原文:http://gunnarpeipman.com/2014/11/asp-net-5-new-configuration-files-and-containers/ ASP.NET vNext提 ...
- 【AngularJS】—— 7 模块化
AngularJS有几大特性,比如: 1 MVC 2 模块化 3 指令系统 4 双向数据绑定 那么本篇就来看看AngularJS的模块化. 首先先说一下为什么要实现模块化: 1 增加了模块的可重用性 ...
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- HDU 1048
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char ...
- [原创]C 语言select函数
参考链接:http://www.cnblogs.com/GameDeveloper/p/3406565.html 注意点: select() 只是执行一次的超时检测.重新进行select要重新设置“超 ...
- 如何把你的图标转换成web字体
在这篇教程中,我们将使用一个免费的Web应用程序IcoMoon将矢量图转换成Web字体,然后将生成的字体通过css应用到Web页面中. 通常我们在网站中必不可少的会使用到一些小图标.在正常尺寸下,布局 ...
- Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示
解决方法: 转自 http://www.cnblogs.com/tc310/archive/2012/12/21/2828450.html http://jingyan.baidu.com/artic ...
- iOS开发——高级篇——Parse 教程:网络后台基础
本教程已针对Swift, iOS 8.3, Xcode 6.3及最新的Parse SDK(1.7.1版本)更新. 网络后台支持可以为你的App添加许多崭新的功能:不论是数据同步,社交分享,还是云端存储 ...
- 迟来的Android的Camera开发总结
这是好久前写的项目,但一直没有去总结.刚好在准备找工作这段时间来总结自己做过的东西,学到的东西. 写Android的自定义的相机应用时,首先要知道一些Camera开发必须知道的尺寸,不然在调试的时候, ...