当前位置: 首页 > news >正文

LeetCode -- 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之间的数字做与运算。


思路:
本题的关键在于,求出了n& n-1为v,就可以直接把n设为v,而没必要在求v与n-1之间的数了。


实现代码:


public class Solution {
    public int RangeBitwiseAnd(int m, int n) 
    {
        while(n > m){
            n = n & n-1;
        }
        return n & m ;
    }
}


相关文章:

  • C 符号列表
  • LeetCode -- Linked List Cycle II
  • LeetCode -- LRU Cache
  • [Web 开发] 定制IE下载对话框的按钮(打开/保存)
  • LeetCode -- Min Stack
  • SQL2005CLR函数扩展-繁简转换
  • LeetCode -- Minimum Size Subarray Sum
  • LeetCode -- Number of 1 Bits
  • 对象属性拷贝(全匹配拷贝)
  • LeetCode -- Reorder List
  • 最近
  • LeetCode -- Search a 2D Matrix II
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • LeetCode -- 3Sum Closest
  • 使用反射将业务对象绑定到 ASP.NET 窗体控件
  • Android开源项目规范总结
  • CAP 一致性协议及应用解析
  • download使用浅析
  • MYSQL 的 IF 函数
  • ng6--错误信息小结(持续更新)
  • React Transition Group -- Transition 组件
  • Redis中的lru算法实现
  • SOFAMosn配置模型
  • SpiderData 2019年2月16日 DApp数据排行榜
  • spring学习第二天
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 笨办法学C 练习34:动态数组
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 用Visual Studio开发以太坊智能合约
  • 原生JS动态加载JS、CSS文件及代码脚本
  • raise 与 raise ... from 的区别
  • #define与typedef区别
  • (ibm)Java 语言的 XPath API
  • (pojstep1.1.2)2654(直叙式模拟)
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (编译到47%失败)to be deleted
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (转)EXC_BREAKPOINT僵尸错误
  • (转)编辑寄语:因为爱心,所以美丽
  • (转)一些感悟
  • ***原理与防范
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .net refrector
  • .NET中统一的存储过程调用方法(收藏)
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • ?
  • @31省区市高考时间表来了,祝考试成功
  • @private @protected @public
  • @Resource和@Autowired的区别
  • @ResponseBody
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [100天算法】-不同路径 III(day 73)
  • [8-23]知识梳理:文件系统、Bash基础特性、目录管理、文件管理、文本查看编辑处理...
  • [Android]一个简单使用Handler做Timer的例子