201. 数字范围按位与

201. Bitwise AND of Numbers Range

题目描述

给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。

LeetCode201. Bitwise AND of Numbers Range中等

示例 1:

输入: [5,7]
输出: 4

示例 2:

输入: [0,1]
输出: 0

Java 实现

方法一

class Solution {
public int rangeBitwiseAnd(int m, int n) {
int d = Integer.MAX_VALUE;
while ((m & d) != (n & d)) {
d <<= 1;
}
return m & d;
}
}

方法二

class Solution {
public int rangeBitwiseAnd(int m, int n) {
int count = 0;
while (m != n) {
m >>= 1;
n >>= 1;
count++;
}
return m << count;
}
}

参考资料

LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)的更多相关文章

  1. C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解

    C#版 - Leetcode 201. 数字范围按位与(bitwise AND) - 题解 在线提交: https://leetcode.com/problems/bitwise-and-of-num ...

  2. [Swift]LeetCode201. 数字范围按位与 | Bitwise AND of Numbers Range

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

  3. 区间数字的按位与 Bitwise AND of Numbers Range

    2018-08-13 22:50:51 问题描述: 问题求解: 首先如果m 和 n不相等,那么必然会有至少一对奇偶数,那么必然末尾是0. 之后需要将m 和 n将右移一位,直到m 和 n相等. 本质上, ...

  4. Java实现 LeetCode 201 数字范围按位与

    201. 数字范围按位与 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入 ...

  5. LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range

    1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...

  6. leetcode 201. 数字范围按位与 解题报告

    给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...

  7. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  8. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  9. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

随机推荐

  1. Mybatis框架-update节点元素的使用

    今天我们学习一下mybatis框架中的update节点元素的使用 需求:修改用户表中的一条数据记录,修改编号为21的用户的密码 UserMapper.xml UserMapper.java 编写测试方 ...

  2. python OOP

    object oriented programming 干啥的 1.避免重名(封装) 2.避免代码重复(继承) 3.将复杂的流程抽象地封装起来 4.模块化程度高,应对复杂编程问题 1)划分职责-要做的 ...

  3. Spark Partition

    分区的意义 Spark RDD 是一种分布式的数据集,由于数据量很大,因此它被切分成不同分区并存储在各个Worker节点的内存中.从而当我们对RDD进行操作时,实际上是对每个分区中的数据并行操作.Sp ...

  4. 【NOIP 2017】宝藏 D2 T2

    参考From 传送门 写的很清晰了 AC code: #include <bits/stdc++.h> using namespace std; const int MAXN = 12; ...

  5. zabbix server内存突然飙升

    2019年10月16日22:20:58 十点二十突然内存占满,top查询一个httpd进程占了79%,查询httpd.error.log发现 [Wed Oct 16 10:24:57.578643 2 ...

  6. Configure JSON.NET to ignore DataContract/DataMember attributes

    https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...

  7. Vue.set 向响应式对象中添加响应式属性,及设置数组元素触发视图更新

    一.为什么需要使用Vue.set? vue中不能检测到数组和对象的两种变化: 1.数组长度的变化 vm.arr.length = 4 2.数组通过索引值修改内容 vm.arr[1] = ‘aa’ Vu ...

  8. vue子组件与子组件之前传值-----最简单办法

    1.在main.js中定义一个值(红色为重点) new Vue({ el: '#app', data: { Bus: new Vue() }, router, store, render: h =&g ...

  9. 安装mininet 一直显示 ‘Cloning into openflow'

    问题描述. 安装mininet卡在了下载openflow. git clone --branch 2.2.2 git@github.com:mininet/mininet.git ,然后输入命令./i ...

  10. 使用MSCK命令修复Hive表分区

    set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...