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

首頁 > 學院 > 開發設計 > 正文

LeetCode: Total Hamming Distance

2019-11-10 17:57:44
字體:
來源:轉載
供稿:網友

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.

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

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 虞城县| 余庆县| 永顺县| 北辰区| 紫阳县| 青浦区| 嘉峪关市| 吐鲁番市| 华坪县| 凤山县| 宜兴市| 江华| 监利县| 和顺县| 三亚市| 惠安县| 化德县| 什邡市| 宝坻区| 吴桥县| 栾川县| 隆林| 长顺县| 河北区| 上思县| 郑州市| 郁南县| 高雄县| 迭部县| 乌兰察布市| 佛冈县| 南靖县| 布拖县| 离岛区| 乐安县| 温宿县| 桃园县| 金沙县| 镇坪县| 会宁县| 通江县|