LeetCode -- Bitwise AND of Numbers Range

来源:互联网 发布:淘梦淘宝大学教程网 编辑:程序博客网 时间:2024/06/01 08:59
题目描述:
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之间的数字做与运算。


思路:
本题的关键在于,求出了n& n-1为v,就可以直接把n设为v,而没必要在求v与n-1之间的数了。


实现代码:


public class Solution {    public int RangeBitwiseAnd(int m, int n)     {        while(n > m){            n = n & n-1;        }        return n & m ;    }}


1 0
原创粉丝点击