1. 程式人生 > >LeetCode 477. Total Hamming Distance-python

LeetCode 477. Total Hamming Distance-python

題目大意:

兩個整數的漢明距離是指其二進位制不相等的位的個數。

計算給定的整數陣列兩兩之間的漢明距離之和。

注意:

  1. 元素大小在[0, 10^9]之間。
  2. 陣列長度不超過10^4。

解題思路:

按位統計各整數的二進位制0與1的個數之和,分別記為zero[i], 和one[i]

ans = ∑(zero[i] * one[i]),  i∈[0, 31]

Python程式碼:

class Solution(object):
    def totalHammingDistance(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
ans = 0 for x in range(32): mask = 1 << x zero = one = 0 for num in nums: if num & mask: one += 1 else: zero += 1 ans += zero * one return ans
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

相關推薦

LeetCode 477. Total Hamming Distance-python

題目大意: 兩個整數的漢明距離是指其二進位制不相等的位的個數。 計算給定的整數陣列兩兩之間的漢明距離之和。 注意: 元素大小在[0, 10^9]之間。陣列長度不超過10^4。 解題思路: 按

LeetCode 477: Total Hamming Distance

blog gdi spa count [] bit min tin cal Note: 1. Very smart way of calculating how many difference from bits: https://en.wikipedia.org/wiki

477. Total Hamming Distance 總的漢明距離

blog += all out xpl bject end min which The Hamming distance between two integers is the number of positions at which the correspondi

477 Total Hamming Distance 漢明距離總和

CP 一個數 logs lee pub 範圍 -h ble min 兩個整數的 漢明距離 指的是這兩個數字的二進制數對應位不同的數量。計算一個數組中,任意兩個數之間漢明距離的總和。示例:輸入: 4, 14, 2輸出: 6解釋: 在二進制表示中,4表示為0100,14表示為1

477. Total Hamming Distance

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

[LeetCode] Total Hamming Distance 全部漢明距離

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 Ha

Total Hamming Distance

題目1 : Total Hamming Distance 時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB 描述 The Hamming Distance between two integers is the number of different di

LeetCode】461 Hamming Distance

漢明距離指的是兩個數字的二進位制表示間對應位置的不同數字的個數。 解法一 直接解法:迴圈依次判斷x和y的對應位是否相同,由異或判斷是否相同: int hammingDistance1(int x, int y) { int coun

LeetCode】461. Hamming Distance

Problem:The Hamming distance between two integers is the number of positions at which the correspondi

LeetCode題解:Hamming Distance

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

LeetCode】461. Hamming Distance【E】【90】

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

[Leetcodepython] Hamming Distance 漢明距離

問題描述: The Hamming distance between two integers is the number of positions at which the correspondin

[LeetCode] Hamming Distance

block 不同 exp 求解 calc example bsp span sta The Hamming Distance between two integers is the number of positions at which the corresponding

461. Hamming Distanceleetcode

數字 code bits 不同之處 res microsoft 解決 ger 原理 The Hamming distance between two integers is the number of positions at which the corresponding

Leetcode - 461. Hamming Distance n&=(n-1) (C++)

blog sta min topic problem discuss logs c++ esc 1. 題目鏈接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常規做法做完看到評論區一個非常有

leetcode 461. Hamming Distance

distance leet min code return while etc ret clas class Solution { public: int hammingDistance(int x, int y) { int res =

leetcode-461-漢明距離(hamming distance)-java

題目及測試 package pid461; /* 漢明距離 兩個整數之間的漢明距離指的是這兩個數字對應二進位制位不同的位置的數目。 給出兩個整數 x 和 y,計算它們之間的漢明距離。 注意: 0 ≤ x, y < 231. 示例: 輸入: x = 1, y = 4

leetcode 461 Hamming Distance

  題目:求兩個整數x,y的海明碼的距離。 思路: 把數字變成海明碼 根據兩個陣列計算海明碼的不同的個數 程式碼: public int hammingDistance(int x, int y) { ArrayList<D

leetcode-461 Hamming Distance

英文 The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Input: x = 1,

LeetCode-Hamming Distance

Description: The Hamming distance between two integers is the number of positions at which the corres