【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 range, inclusive.
For example, given the range [5, 7], you should return 4.
题意:
给定 [m, n] 范围内个数,返回范围内所有数相与的结果。
思路:
如果打着暴力的旗号,那么这个题是会超时的 - -!。最后也是没脾气了,看了别人的blog,代码很呆萌,原理也很朴实:当m != n时,最末位必定等于0,因为[m,n]必定包含奇偶数,相与最末位等0,这时m和n同时右移一位(offset++);直到当m = n的时候,此时想与已经不会改变什么了,此时的m或者n左移offset位即为结果(好像似乎可能明白了什么,但是好像又什么也没明白 - -!当定理背过如何?)。
C++:
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int offset = ;
while(m!=n){
m>>=;
n>>=;
offset++;
}
return m<<offset;
}
};
Python:
class Solution:
# @param {integer} m
# @param {integer} n
# @return {integer}
def rangeBitwiseAnd(self, m, n):
offset = 0 while m != n:
m = m >> 1
n = n >> 1
offset = offset + 1 ret = m << offset return ret
【LeetCode 201】Bitwise AND of Numbers Range的更多相关文章
- 【leetcode】Bitwise AND of Numbers Range(middle)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode(201) Bitwise AND of Numbers Range
题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...
- 【leetcode❤python】 165. Compare Version Numbers
#-*- coding: UTF-8 -*-class Solution(object): def compareVersion(self, version1, version2): ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】201. Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...
- 【刷题-LeetCode】201 Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- oracle的全文索引
1.查看oracle的字符集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ------------------- ...
- Android 监测手机联网状态 wifi、移动数据流量、无联网状态
手机当完成联网时会发送一个广播,我们只要创建一个广播接收者即可,代码如下: package com.example.NetworkChangeReceiver2; import android.con ...
- cojs 安科赛斯特 题解报告
QAQ 从IOI搬了一道题目过来 官方题解貌似理论上没有我的做法优,我交到BZOJ上也跑的飞快 结果自己造了个数据把自己卡成了4s多,真是忧桑的故事 不过貌似原题是交互题,并不能离线 说说我的做法吧 ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- 一个zip压缩类,欢迎吐槽
package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...
- ios摇一摇
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (motion==UIEventSubtypeMo ...
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- Android开发之assets文件夹中资源的获取
assets中的文件都是保持原始的文件格式,需要使用AssetManager以字节流的形式读取出来 步骤: 1. 先在Activity里面调用getAssets() 来获取AssetManager引用 ...
- chrome浏览器无法设置打开特定网页
最近chrome浏览器更新后,发现以前设置的启动浏览器“重上次停下的地方继续”功能消失了. 当我点击设置网页时,会出现如上提示. 后来有同事给了如下一个连接,里面说到这个是公司的超级管理员搞的,他定义 ...
- PL/SQL database character set(AL32UTF8) and Client character set(ZHS16GBK) are different 2012-04-11 13:01
启动PL/SQL Developer 报字符编码不一致错误 Database character set (AL32UTF8) and Client character set (ZHS16GBK) ...