国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 肇庆市| 隆昌县| 麦盖提县| 儋州市| 密云县| 大竹县| 大宁县| 弥勒县| 姜堰市| 石城县| 庆云县| 晋州市| 乐东| 渭源县| 三江| 河东区| 宁化县| 得荣县| 鹤峰县| 军事| 肥乡县| 潮安县| 容城县| 和硕县| 中西区| 闽清县| 黔南| 乌拉特中旗| 陕西省| 常山县| 莱州市| 塔河县| 渭南市| 长汀县| 油尖旺区| 黎城县| 青阳县| 侯马市| 昭觉县| 无棣县| 崇仁县|