201-Bitwise AND of Numbers Range
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.
問題描述
給定範圍[m, n], 0 <= m <= n <= 2147483647, 返回範圍內所有整數的按位交的值。
問題分析
我的理解是求出m 和 n的位表示的字首, 然後在字首後面加上0
解法
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int i = 0;
//迭代求出字首, 注意這裡退出迴圈的條件為m != n
//i為右邊需要新增的0的個數
while(m != n){
m >>= 1;
n >>= 1;
i++;
}
//在字首後面新增0
return m << i;
}
}
相關文章
- Leetcode - Bitwise AND of Numbers RangeLeetCode
- LeetCode-Bitwise AND of Numbers RangeLeetCode
- 400多種Numbers模板 DesiGN for Numbers Templates for macMac
- Codeforces - Jzzhu and Numbers
- different random numbers generatorrandom
- Collecting Numbers II
- HTML input rangeHTML
- Python range()Python
- Range Sparse Net
- Convert Range-Partitioned Table To Interval-Range-Partitioned Table
- LeetCode-Lexicographical NumbersLeetCode
- Find All Numbers Disappeared in an ArrayAPP
- Golang for range的坑Golang
- MySQL RANGE分割槽MySql
- golang range的用法Golang
- MySQL range問題MySql
- Global Range Partitioned IndexesIndex
- B. Range and Partition
- python-rangePython
- Range Minimum Sum
- http斷點續傳原理:http頭 Range、Content-RangeHTTP斷點
- Leetcode Sum Root to Leaf NumbersLeetCode
- Leetcode Add Two NumbersLeetCode
- Quick Reference to Patch Numbers for Database PSUUIDatabase
- Go-For Range 效能研究Go
- The Range of Application2APP
- GO 的 range 如何使用?Go
- Leetcode Search for a RangeLeetCode
- Index Range Scan (214)Index
- LeetCode 2 Add Two NumbersLeetCode
- Bitcoin Node Numbers Fall After Spam Transaction "Attack"
- Add_Two_Numbers python 求解Python
- 448. Find All Numbers Disappeared in an ArrayAPP
- Leetcode-Add Two NumbersLeetCode
- Sum Root to Leaf Numbers leetcode javaLeetCodeJava
- Add Two Numbers leetcode javaLeetCodeJava
- How to Convert Decimal Numbers to Words with PythonDecimalPython
- Quick Reference to Patchset Patch Numbers [ID 753736.1]UI