LeetCode Bitwise AND of Numbers Range
来源:互联网 发布:135端口入侵 编辑:程序博客网 时间:2024/05/20 03:36
Description:
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.
Solution:
First, we should be clear that the answer should be no larger than both m and n.
Then we can just move the digit from ones to 32nd, loop continues until it reaches the condition that n equals m. And there might be the condition that n will never be equal to m, so just return 0.
<span style="font-size:18px;">import java.util.*;public class Solution {public int rangeBitwiseAnd(int m, int n) {int offset = 0;while (m > 0 && n > 0) {if (m == n)return n << offset;offset++;m >>= 1;n >>= 1;}return 0;}}</span>
0 0
- 【2015_07_09】迷失
- LeetCode228——Summary Ranges
- dedecms(织梦)在后台新建新的模型时 ,点击编辑的时候 更新时间不能弹出
- Java字符参与运算和字符串参与运算
- 初次使用cocoapods注意事项
- LeetCode Bitwise AND of Numbers Range
- CDOJ 763 树上的鸟儿(水题/模拟题)
- LeetCode Happy Number
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) D. Restoring Numbers 构造 数学
- 学习STL过程中的一些零散记录4
- EularProject 34: 一个数字与他每位数的阶乘和
- 十六进制色码表
- roboguice2教程
- Sqlite中如何返回唯一数据