LeetCode -- Bitwise AND of Numbers Range

原创 2015年11月21日 10:10:22
题目描述:
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 ;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Leetcode 201 Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this ...

Leetcode 201 Bitwise AND of Numbers Range 区间内整数按位与

给出一个区间 [m, n],其中0 <= m <= n <= 2147483647,即m,n都是非负整数,返回这个区间内所有整数的按位与结果,包括m和n在内.

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

leetcode 201:Bitwise AND of Numbers Range

Question:   Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num...

LeetCode 201 Bitwise AND of Numbers Range

Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4. 思路:介于(包括...

Leetcode 201 - Bitwise AND of Numbers Range(bit & math)

题意给定两个数m和n,并且m≤nm \leq n,求f(m, n) = m & (m + 1) & (m + 2) & ... & n的值。思路算法1我们知道,在范围[m,n][m, n]内,对于二进...
  • Lzedo
  • Lzedo
  • 2017-03-10 17:45
  • 93

[Leetcode] 201. Bitwise AND of Numbers Range 解题报告

题目: Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4. ...

Leetcode-201- Bitwise AND of Numbers Range

Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.  题意:m到n若干个数字...

leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

链接:https://leetcode.com/problems/number-of-1-bits/ 此题关键是如何判断一个数字的第i为是否为0  即: x& (1 class Solution { ...

201 Bitwise AND of Numbers Range

题目介绍:将m到n之间的所有的数相与,得到其结果 要点:最笨的方法是将所有的数真正的相与,这无疑耗时效率低下。通过分析可以发现,若两个数最高有效位长度不相等,则最终结果一定是 仅含n的最高有效位的值,...

LeetCodeOJ_201_Bitwise AND of Numbers Range

Given a range [m, n]where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbersin this ra...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)