- 浏览: 173189 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 222You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 426The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 383Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 514Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 598Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 731You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ...
相关推荐
LeetCode 201的题目是"数字范围按位与"(Bitwise AND of Numbers Range),其核心要求是给定两个整数(记为m和n),需要找出这两个数范围内所有数的按位与(bitwise AND)结果。例如,给定范围[5, 7],结果应为4。 ...
Bitwise AND of Numbers Range Power of Three Rectangle Area 数论 Happy Number Ugly Number Ugly Number II Super Ugly Number Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse ...
[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) | _...
OpenCV-Python图像位与运算bitwise_and函数详解.rar
poj 3495 Bitwise XOR of Arithmetic Progression.md
Programming Interview Problems and Algorithms in Ruby by Zachary Paul English | April 17, 2016 | ASIN: B01EGILLLS | 177 Pages ... Bitwise Operations, Dynamic Programming, and many others.
The decoding algorithm used in RBDS.c is based on error... The program emulates the operation of the encoder and decoder of a binary cyclic codes, using bitwise shifts and xor for modulo g(x) operations.
A test harness that checks a student's solution in bits.c
Bitwise是一个教育项目,能让我们从头开始为计算机创建软件/硬件堆栈
Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and offers the purchaser a $50 discount off the price of buying the four volumes individually. The Art of Computer ...
A basic understanding of these ideas is essential to any JavaScript developer wishing to analyze and build great software solutions. You’ll discover how to implement data structures such as hash ...
使用布尔BitBlt操作在屏幕的位平面上显示并行的Conway's Life游戏的视觉效果。 (由BitBlt的发明者在Smalltalk-80 Blue Book中已经给出的独立重新发现的算法。)
按位与 ( bitwise and of x and y ) & 举例: 5&3 = 1 解释: 101 11 相同位仅为个位1 ,故结果为 1 按位或 ( bitwise or of x and y ) | 举例: 5|3 = 7 解释: 101 11 出现1的位是 1 1 1,故结果为 ...
bitwise.zip
Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4:...
max of `32`, min of `0`.const rotationObject = bitwiseRotation ( 8 ) ;// Returns an object with rotate right (`ror`) and rotate left (`rol`) methods.const { ror , rol } = rotationObject ;// Rotate a ...
按位 JavaScript / TypeScript库可操作位,半字节,字节和... and ( [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 1 ] ) // [0, 0, 0, 1] bitwise . bits . xor ( [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 0 , 1 ] ) // [0, 1, 1, 0] //
Precedence and Order of Evaluation Chapter 3: Control Flow Statements and Blocks If-Else Else-If Switch Loops - While and For Loops - Do-While Break and Continue Goto and labels Chapter 4:...