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

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

LeetCode: Total Hamming Distance

2019-11-09 20:17:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

Input: 4, 14, 2Output: 6Explanation: In binary rePResentation, the 4 is 0100, 14 is 1110, and 2 is 0010 (justshowing the four bits relevant in this case). So the answer will be:HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

Note:

Elements of the given array are in the range of to 10^9

Length of the array will not exceed 10^4.

思路:一個(gè)有多個(gè)0,1組成的序列,所有的漢明距離和為0的個(gè)數(shù)乘以1的個(gè)數(shù)

int totalHammingDistance(int* nums, int numsSize) {        int totalD = 0;    int bits[32];    for (int i = 0; i < 32; ++i) {        bits[i] = 0;    }        for (int i = 0; i < numsSize; ++i) {        for (int j = 0; j < 32; ++j) {            bits[j] += nums[i] & 1;            nums[i] >>= 1;        }    }        for (int i = 0; i < 32; ++i) {        totalD += bits[i] * (numsSize - bits[i]);    }        return totalD;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 乡城县| 漳浦县| 五河县| 化隆| 长丰县| 家居| 芷江| 疏勒县| 阿拉善左旗| 泌阳县| 普兰店市| 宁城县| 正镶白旗| 大关县| 宝坻区| 汉川市| 乌兰县| 德安县| 湘阴县| 塘沽区| 京山县| 乾安县| 大英县| 邻水| 思南县| 商城县| 微博| 永平县| 临海市| 富平县| 香格里拉县| 永泰县| 改则县| 缙云县| 嘉鱼县| 隆子县| 西畴县| 赤壁市| 乐昌市| 满城县| 南漳县|