leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
生活随笔
收集整理的這篇文章主要介紹了
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 in this range, inclusive.
For example, given the range [5, 7], you should return 4.
題解:如果m==n,那么答案就是m.
如果m<n,那么二進制最右邊一位在最后的結果中肯定是0,那么就可以轉化成子問題:
rangeBitwiseAnd(m>>1, n>>1)?
class Solution { public:int rangeBitwiseAnd(int m, int n) {return (n > m) ? (rangeBitwiseAnd(m>>1, n>>1) << 1) : m;} };?
轉載于:https://www.cnblogs.com/zywscq/p/5429171.html
總結
以上是生活随笔為你收集整理的leetcode 201. Bitwise AND of Numbers Range(位运算,dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 注释
- 下一篇: CentOS中输入yum报错:sudo: