标签:
原题链接在这里:https://leetcode.com/problems/bitwise-and-of-numbers-range/
其实就是找n最大的bit上对应的m的bit是否为1, 若不是1, 就返回0; 若是1, 就返回那个bit上为1后面为0的数字,如100=4.
AC Java:
1 public class Solution { 2 public int rangeBitwiseAnd(int m, int n) { 3 //寻找相同最大位置是否都是1 4 int count = 0; 5 while(m != n){ 6 m = m>>1; 7 n = n>>1; 8 count++; 9 } 10 return m<<count; 11 } 12 }
LeetCode Bitwise AND of Numbers Range
标签:
原文地址:http://www.cnblogs.com/Dylan-Java-NYC/p/4919770.html