【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/ 题目地 ...
随机推荐
- Teradata SQL programming
Teradata的SQL设计和Oracle真不是一个水平, 一点美感的没有. 上个世纪它靠着MPP一招鲜吃变天, 居然做了十多年数据仓库的老大, 时过境迁, 现在有不少SQL On Hadoop ...
- Mysql CPU占用90%
今天网站打开卡,查了下发现Mysql CPU占用到90%! 1.通过putty工具连接mysql putty工具十分的强大,它可以让我们直接访问MySQL.当然,要实现这一步肯定要做点什么.MySQL ...
- web api 处理发送过来的文件(图片)
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.We ...
- Environment 常用方法
* 方法:getDataDirectory()解释:返回 File ,获取 Android 数据目录.* 方法:getDownloadCacheDirectory()解释:返回 File ,获取 An ...
- Hadoop Fsimage 和 editlog
在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...
- su root 和su - root 的区别
su - root is the same as su - just like login as root, then the shell is login shell,which mean i ...
- 用hexo书写github.io博客 学习心得 教程
很久没更新文章了,除了工作忙之外,可能就是自己懒惰了. 最近混迹与github,发现git上写博客也是个很不错的平台. 推荐使用 hexo 模版来书写,毕竟我们重点是写文章,而不是管理,所以有神奇何妨 ...
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...
- python 内置函数 : compile()
这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. 参数source是一串字符串的源码,或者是 ...
- (苹果AppleWWDRCA.cer证书过期)Failed to locate or generate matching signing assets
从2月14号开始,上传AppStore会碰到:Failed to locate or generate matching signing assets 字数462 阅读13571 评论16 喜欢61 ...