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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

461. Hamming Distance

2019-11-08 19:30:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Example:

Input: x = 1, y = 4Output: 2Explanation:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑The above arrows point to positions where the corresponding bits are different.class Solution {public: int hammingDistance(int x, int y) { int cnt = 0; while(x || y){ if((x & 1) != (y & 1)) ++cnt; x >>= 1; y >>= 1; } return cnt; }};

先異或,再轉(zhuǎn)換成有多少個(gè)1

class Solution {public: int hammingDistance(int x, int y) { int dist = 0, n = x ^ y; while (n) { ++dist; n &= n - 1; } return dist; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 军事| 三门峡市| 怀集县| 珲春市| 靖边县| 永福县| 浮梁县| 浦东新区| 新河县| 墨江| 江津市| 嘉兴市| 比如县| 武功县| 杭锦后旗| 闽清县| 湖口县| 宿松县| 东乌| 东阿县| 黔东| 景德镇市| 浮山县| 盱眙县| 珠海市| 罗城| 西乌珠穆沁旗| 金门县| 九寨沟县| 大安市| 金溪县| 沛县| 安泽县| 湟中县| 绵阳市| 尚志市| 明水县| 平阴县| 丽水市| 上蔡县| 郁南县|