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

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

LeetCode: Total Hamming Distance

2019-11-09 19:31:25
字體:
來源:轉載
供稿:網(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.

思路:一個有多個0,1組成的序列,所有的漢明距離和為0的個數(shù)乘以1的個數(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;}


上一篇:SAP 筆記

下一篇:矩形的個數(shù)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陆良县| 盐城市| 香河县| 佳木斯市| 宜良县| 资溪县| 株洲市| 正镶白旗| 姜堰市| 甘德县| 阿尔山市| 根河市| 象山县| 澜沧| 克什克腾旗| 寻甸| 博爱县| 桐庐县| 剑河县| 苍南县| 上高县| 泸定县| 凤阳县| 丽江市| 滦南县| 高要市| 若羌县| 辽中县| 平原县| 南雄市| 万州区| 金堂县| 霞浦县| 慈利县| 忻城县| 本溪市| 资阳市| 衡山县| 大化| 沙田区| 历史|