477. Total Hamming Distance Medium

@problem@discussion
#Array#Math#Bit Manipulation



1/**
2 * [477] Total Hamming Distance
3 *
4 * The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.
5 * Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [4,14,2]
10 * Output: 6
11 * Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
12 * showing the four bits relevant in this case).
13 * The answer will be:
14 * HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
15 * 
16 * Example 2:
17 * 
18 * Input: nums = [4,14,4]
19 * Output: 4
20 * 
21 *  
22 * Constraints:
23 * 
24 * 	1 <= nums.length <= 10^4
25 * 	0 <= nums[i] <= 10^9
26 * 	The answer for the given input will fit in a 32-bit integer.
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/total-hamming-distance/
32// discuss: https://leetcode.com/problems/total-hamming-distance/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_477() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.