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;
}
}
相關文章
- Reversed Numbers
- 400多種Numbers模板 DesiGN for Numbers Templates for macMac
- Collecting Numbers II
- Codeforces - Jzzhu and Numbers
- different random numbers generatorrandom
- 【Lintcode】1267. Lexicographical Numbers
- 829. Consecutive Numbers Sum
- 165. Compare Version Numbers
- Leetcode 165 Compare Version NumbersLeetCode
- Find All Numbers Disappeared in an ArrayAPP
- LeetCode 2 Add Two NumbersLeetCode
- Range Minimum Sum
- python-rangePython
- Range Sparse Net
- for range 作用域
- HTML input rangeHTML
- http斷點續傳原理:http頭 Range、Content-RangeHTTP斷點
- CF1406E Deleting Numbers
- B. Numbers Box(思維)
- C. k-Amazing Numbers
- LeetCode-2 Add Two NumbersLeetCode
- Sum of Square Numbers 平方數之和
- Self Dividing Numbers 自除數
- Note 886532 - Pricing: Displaying and rounding numbers
- Leetcode 967 Numbers With Same Consecutive DifferencesLeetCode
- LeetCode 2. Add Two NumbersLeetCode
- 129-Sum Root to Leaf Numbers
- Add_Two_Numbers python 求解Python
- B. Range and Partition
- for & range 效能對比
- Golang for range的坑Golang
- The Range of Application2APP
- Range/Content-Range與斷點續傳,瞭解一下?斷點
- [題解]SP10606 Balanced Numbers
- LeetCode 129. Sum Root to Leaf NumbersLeetCode
- 421-Maximum XOR of Two Numbers in an Array
- ABAP Range 選擇表
- GO 的 range 如何使用?Go