- 浏览: 183435 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定一个范围,通过位与运算计算这个范围内所有的整数,返回计算后的结果。我们只需要从两个数的高位开始比较,如果遇到不相等的就停止,因为当这个范围内所有的数位与之后,只有高位相等且为1的情况下,这些位上的数才为1,其它都为0。我们借助一个help,long help = 1L << 31,让它从m和n的最高位开始比较。这里需要让help为长整型,因为int是有符号数,如果最高位为1,右移之后高位补1,而我们需要让高位补0,因此设定为长整形。代码如下:
For example, given the range [5, 7], you should return 4.
给定一个范围,通过位与运算计算这个范围内所有的整数,返回计算后的结果。我们只需要从两个数的高位开始比较,如果遇到不相等的就停止,因为当这个范围内所有的数位与之后,只有高位相等且为1的情况下,这些位上的数才为1,其它都为0。我们借助一个help,long help = 1L << 31,让它从m和n的最高位开始比较。这里需要让help为长整型,因为int是有符号数,如果最高位为1,右移之后高位补1,而我们需要让高位补0,因此设定为长整形。代码如下:
public class Solution { public int rangeBitwiseAnd(int m, int n) { long help = 1L << 31; int result = 0; for(; help > 0; help >>= 1) { if((m & help) == (n & help)) result += (n & help); else break; } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
LeetCode 201的题目是"数字范围按位与"(Bitwise AND of Numbers Range),其核心要求是给定两个整数(记为m和n),需要找出这两个数范围内所有数的按位与(bitwise AND)结果。例如,给定范围[5, 7],结果应为4。 ...
c++ C++_Leetcode题解之201_bitwise_and_of_numbers_range.cpp
[Bitwise AND of Numbers Range]() - 尚未上传 [LRU Cache]() - 尚未上传 [Jump Game]() - 尚未上传 []() - 尚未上传 []() - 尚未上传 []() - 尚未上传 第 5 周: []() - 尚未上传 []() - 尚未上传 []() - 尚未上传 ...
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...
course delves into the realm of arithmetic operations in computer systems, specifically focusing on integer representations and floating-point numbers. This chapter is crucial for understanding how ...
Negative numbers are represented by taking the bitwise complement of their absolute value and adding one. The leading bit, also known as the most significant bit (MSB), is 0 for positive and zero ...
It is structured to cater to the needs of students pursuing various engineering and scientific disciplines, where the use of C programming can range from being a supportive topic to a core component ...
The section on low-level algorithms covers a wide range of topics related to bit manipulation and word-level operations. Here are some of the key insights and algorithms discussed: 1. **Bit Wizardry*...
Negative numbers are depicted by taking the bitwise complement (one's complement) of their absolute value and adding 1. For example, to represent -5 in a 4-bit two's complement system, the process ...
Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API ...
- Combine hash codes using prime numbers or bitwise operations. **Item 8: Prefer Query Syntax to Loops** - **Advantages:** Query syntax is more readable and expressive than traditional loops. - **...
The number of significant digits displayed in floating point numbers. ; http://php.net/precision precision = 14 ; Output buffering is a mechanism for controlling how much output data ; (excluding ...
- 其他操作符,包括展开操作符(Spread operator)、范围操作符(Range operator)、子脚本操作符(Subscript operator)、成员资格操作符(Membership operator)、身份操作符(Identity operator)、强制转换操作...
5.9 Preprocessing numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 5.10 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...