https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA)

面试官,你再问我 Bit Operation 试试?

描述

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

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

如:

输入: [26,30]
输出: 24

解析

首先,将 [ 26 , 30 ] 的范围数字用二进制表示出来:

11010  11011  11100  11101  11110

而输出 24 的二进制是 11000 。

可以发现,只要找到二进制的 左边公共部分 即可。

所以,可以每次向左移一位,比较 m 和 n 是否相同,不同再继续左移一位,直至相同,然后相同的数再左移对应位数即可。

代码

public int rangeBitwiseAnd(int m, int n) {
int i = 0; // i means we have how many bits are 0 on the right
while(m != n) {
m >>= 1;
n >>= 1;
i++;
}
return m << i;
}

另一种:

可以先建立一个 32 位都是 1 的 mask,然后每次向左移一位,比较 m 和 n 是否相同,不同再继续左移一位,直至相同,然后把 m 和 mask 相与就是最终结果。

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

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

  1. 201 Bitwise AND of Numbers Range 数字范围按位与

    给定范围 [m,n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含m, n两端点).例如,给定范围 [5,7],您应该返回 4. 详见 ...

  2. Leetcode201. Bitwise AND of Numbers Range数字范围按位与

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

  3. Java for LeetCode 201 Bitwise AND of Numbers Range

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

  4. [LeetCode#201] Bitwise AND of Numbers Range

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

  5. leetcode 201. Bitwise AND of Numbers Range(位运算,dp)

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

  6. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0

    https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...

  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. Oracle DBHelper 第二版

    public static class OracleDBHelper { public static OracleCommand cmd = null; public static OracleCon ...

  2. 【Selenium2】【Python多线程】

    # all_tests_pro.py import unittest,time,os,multiprocessingimport HTMLTestRunner #查找多有含有thread的文件,文件夹 ...

  3. Spring中JdbcTemplate使用RowMapper

    package com.cxl.demo.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util. ...

  4. sklearn.learning_curve

    学习曲线函数: from sklearn.learning_curve import learning_curve 调用格式: learning_curve(estimator, X, y, trai ...

  5. jmeter线程组之间传参

    背景介绍: 使用jmeter做登录和搜索接口的测试: 登录接口请求头为:Content-Type: application/x-www-form-urlencoded; charset=UTF-8 搜 ...

  6. @Value注解分类解析

    1.1.1            @Value注解 @Value的作用是通过注解将常量.配置文件中的值.其他bean的属性值注入到变量中,作为变量的初始值. (1)常量注入 @Value(" ...

  7. iframe和ajax文件上传方法

    为什么使用这两种方法文件上传呢,主要是因为局部刷新问题 上传问题,主要是使用form表单,或者用请求一个文件上传 iframe 大多网站都是有一个整体的固定结构,然后进行局部刷新,我们可以使用AJAX ...

  8. lua --- 函数的本质

    1.lua中的函数是带有此法界定的第一类值. 2.创建一个函数的过程,本质上就是一个创建赋值语句的过程. 常见的创建函数的过程: function fun() print("Hello wo ...

  9. SVN-版本控制工具安装与使用

    什么是版本控制? 版本控制(Revision control)是一种软体工程技巧,籍以在开发的过程中,确保由不同人所编辑的同一档案都得到更新. 版本控制透过文档控制(documentation con ...

  10. cookie知识点

    1.springmvc框架中,cookie例子 jsp: <%-- Created by IntelliJ IDEA. User: 44262 Date: 2019/2/28 Time: 18: ...