标签:
题目地址:https://leetcode.com/problems/bitwise-and-of-numbers-range/
题目解析:参考https://leetcode.com/problems/number-of-1-bits/,我们知道n&n-1为n和n-1两者左边相同的部分,那么容易得到n-1&n-2为n-2与n-1左边相同部分,依次类推,m&m+1....&n为m与n左边相同部分。
题目解答:
public class Solution { public int rangeBitwiseAnd(int m, int n) { int steps = 0; while(m!=n){ m>>=1; n>>=1; steps++; } return m<<steps; } }
Leetcode Bitwise AND of Numbers Range
标签:
原文地址:http://www.cnblogs.com/xiongyuesen/p/4438379.html