国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

201. Bitwise AND of Numbers Range

2019-11-08 19:49:25
字體:
供稿:網(wǎng)友

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.

解題思路:基數(shù)和偶數(shù)的and結(jié)果末位為0。為了找m,n之間的公共前綴,mn分別右移,并記錄次數(shù)。

public class Solution { public int rangeBitwiseAnd(int m, int n) { if (m == 0) return 0; int factor = 1; while (m != n) { m >>= 1; n >>= 1; factor <<= 1; } return m*factor; }}public class Solution { public int rangeBitwiseAnd(int m, int n) { if (m == 0) return 0; int step = 0; while (m != n) { m >>= 1; n >>= 1; step++; } return (m<<step); }}public class Solution { public int rangeBitwiseAnd(int m, int n) { if (m == 0) return 0; int step = 0; while (m < n) { n &= (n-1); } return n; }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 家居| 仁化县| 泰顺县| 卓尼县| 安吉县| 江永县| 岗巴县| 仙游县| 梓潼县| 普兰店市| 禹城市| 彝良县| 麦盖提县| 大港区| 色达县| 龙陵县| 康平县| 台北市| 江川县| 武宁县| 新源县| 宕昌县| 滁州市| 巍山| 盐城市| 广平县| 东至县| 岐山县| 湟源县| 灵璧县| 黄平县| 贵南县| 余江县| 濮阳市| 简阳市| 扬中市| 吉木乃县| 长泰县| 蕲春县| 新蔡县| 西充县|