Skip to content

Commit 128ef00

Browse files
authored
feat: add solutions to lc problem: No.0477 (#2760)
1 parent f127e2c commit 128ef00

File tree

8 files changed

+159
-97
lines changed

8 files changed

+159
-97
lines changed

solution/0400-0499/0477.Total Hamming Distance/README.md

+56-32
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,33 @@ HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2
4545

4646
### 方法一:位运算
4747

48+
我们在 $[0, 31]$ 的范围内枚举每一位,对于当前枚举的位 $i$,我们统计所有数字中的第 $i$ 位为 $1$ 的个数 $a$,那么这些数字中的第 $i$ 位为 $0$ 的个数就是 $b = n - a$,其中 $n$ 是数组的长度。这样的话,在第 $i$ 位上的汉明距离之和就是 $a \times b$,我们把所有的位的汉明距离相加即为答案。
49+
50+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组的长度和数组中的元素的最大值。空间复杂度 $O(1)$。
51+
4852
<!-- tabs:start -->
4953

5054
```python
5155
class Solution:
5256
def totalHammingDistance(self, nums: List[int]) -> int:
53-
ans = 0
54-
for i in range(31):
55-
a = b = 0
56-
for v in nums:
57-
t = (v >> i) & 1
58-
if t:
59-
a += 1
60-
else:
61-
b += 1
57+
ans, n = 0, len(nums)
58+
for i in range(32):
59+
a = sum(x >> i & 1 for x in nums)
60+
b = n - a
6261
ans += a * b
6362
return ans
6463
```
6564

6665
```java
6766
class Solution {
6867
public int totalHammingDistance(int[] nums) {
69-
int ans = 0;
70-
for (int i = 0; i < 31; ++i) {
71-
int a = 0, b = 0;
72-
for (int v : nums) {
73-
int t = (v >> i) & 1;
74-
a += t;
75-
b += t ^ 1;
68+
int ans = 0, n = nums.length;
69+
for (int i = 0; i < 32; ++i) {
70+
int a = 0;
71+
for (int x : nums) {
72+
a += (x >> i & 1);
7673
}
74+
int b = n - a;
7775
ans += a * b;
7876
}
7977
return ans;
@@ -85,14 +83,13 @@ class Solution {
8583
class Solution {
8684
public:
8785
int totalHammingDistance(vector<int>& nums) {
88-
int ans = 0;
89-
for (int i = 0; i < 31; ++i) {
90-
int a = 0, b = 0;
91-
for (int& v : nums) {
92-
int t = (v >> i) & 1;
93-
a += t;
94-
b += t ^ 1;
86+
int ans = 0, n = nums.size();
87+
for (int i = 0; i < 32; ++i) {
88+
int a = 0;
89+
for (int x : nums) {
90+
a += x >> i & 1;
9591
}
92+
int b = n - a;
9693
ans += a * b;
9794
}
9895
return ans;
@@ -101,18 +98,45 @@ public:
10198
```
10299
103100
```go
104-
func totalHammingDistance(nums []int) int {
105-
ans := 0
106-
for i := 0; i < 31; i++ {
107-
a, b := 0, 0
108-
for _, v := range nums {
109-
t := (v >> i) & 1
110-
a += t
111-
b += t ^ 1
101+
func totalHammingDistance(nums []int) (ans int) {
102+
for i := 0; i < 32; i++ {
103+
a := 0
104+
for _, x := range nums {
105+
a += x >> i & 1
112106
}
107+
b := len(nums) - a
113108
ans += a * b
114109
}
115-
return ans
110+
return
111+
}
112+
```
113+
114+
```ts
115+
function totalHammingDistance(nums: number[]): number {
116+
let ans = 0;
117+
for (let i = 0; i < 32; ++i) {
118+
const a = nums.filter(x => (x >> i) & 1).length;
119+
const b = nums.length - a;
120+
ans += a * b;
121+
}
122+
return ans;
123+
}
124+
```
125+
126+
```rust
127+
impl Solution {
128+
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
129+
let mut ans = 0;
130+
for i in 0..32 {
131+
let mut a = 0;
132+
for &x in nums.iter() {
133+
a += (x >> i) & 1;
134+
}
135+
let b = (nums.len() as i32) - a;
136+
ans += a * b;
137+
}
138+
ans
139+
}
116140
}
117141
```
118142

solution/0400-0499/0477.Total Hamming Distance/README_EN.md

+57-33
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,35 @@ HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2
4040

4141
## Solutions
4242

43-
### Solution 1
43+
### Solution 1: Bit Manipulation
44+
45+
We enumerate each bit in the range $[0, 31]$. For the current enumerated bit $i$, we count the number of numbers where the $i$-th bit is $1$, denoted as $a$. Therefore, the number of numbers where the $i$-th bit is $0$ is $b = n - a$, where $n$ is the length of the array. In this way, the sum of the Hamming distance on the $i$-th bit is $a \times b$. We add the Hamming distances of all bits to get the answer.
46+
47+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array and the maximum value in the array, respectively. The space complexity is $O(1)$.
4448

4549
<!-- tabs:start -->
4650

4751
```python
4852
class Solution:
4953
def totalHammingDistance(self, nums: List[int]) -> int:
50-
ans = 0
51-
for i in range(31):
52-
a = b = 0
53-
for v in nums:
54-
t = (v >> i) & 1
55-
if t:
56-
a += 1
57-
else:
58-
b += 1
54+
ans, n = 0, len(nums)
55+
for i in range(32):
56+
a = sum(x >> i & 1 for x in nums)
57+
b = n - a
5958
ans += a * b
6059
return ans
6160
```
6261

6362
```java
6463
class Solution {
6564
public int totalHammingDistance(int[] nums) {
66-
int ans = 0;
67-
for (int i = 0; i < 31; ++i) {
68-
int a = 0, b = 0;
69-
for (int v : nums) {
70-
int t = (v >> i) & 1;
71-
a += t;
72-
b += t ^ 1;
65+
int ans = 0, n = nums.length;
66+
for (int i = 0; i < 32; ++i) {
67+
int a = 0;
68+
for (int x : nums) {
69+
a += (x >> i & 1);
7370
}
71+
int b = n - a;
7472
ans += a * b;
7573
}
7674
return ans;
@@ -82,14 +80,13 @@ class Solution {
8280
class Solution {
8381
public:
8482
int totalHammingDistance(vector<int>& nums) {
85-
int ans = 0;
86-
for (int i = 0; i < 31; ++i) {
87-
int a = 0, b = 0;
88-
for (int& v : nums) {
89-
int t = (v >> i) & 1;
90-
a += t;
91-
b += t ^ 1;
83+
int ans = 0, n = nums.size();
84+
for (int i = 0; i < 32; ++i) {
85+
int a = 0;
86+
for (int x : nums) {
87+
a += x >> i & 1;
9288
}
89+
int b = n - a;
9390
ans += a * b;
9491
}
9592
return ans;
@@ -98,18 +95,45 @@ public:
9895
```
9996
10097
```go
101-
func totalHammingDistance(nums []int) int {
102-
ans := 0
103-
for i := 0; i < 31; i++ {
104-
a, b := 0, 0
105-
for _, v := range nums {
106-
t := (v >> i) & 1
107-
a += t
108-
b += t ^ 1
98+
func totalHammingDistance(nums []int) (ans int) {
99+
for i := 0; i < 32; i++ {
100+
a := 0
101+
for _, x := range nums {
102+
a += x >> i & 1
109103
}
104+
b := len(nums) - a
110105
ans += a * b
111106
}
112-
return ans
107+
return
108+
}
109+
```
110+
111+
```ts
112+
function totalHammingDistance(nums: number[]): number {
113+
let ans = 0;
114+
for (let i = 0; i < 32; ++i) {
115+
const a = nums.filter(x => (x >> i) & 1).length;
116+
const b = nums.length - a;
117+
ans += a * b;
118+
}
119+
return ans;
120+
}
121+
```
122+
123+
```rust
124+
impl Solution {
125+
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
126+
let mut ans = 0;
127+
for i in 0..32 {
128+
let mut a = 0;
129+
for &x in nums.iter() {
130+
a += (x >> i) & 1;
131+
}
132+
let b = (nums.len() as i32) - a;
133+
ans += a * b;
134+
}
135+
ans
136+
}
113137
}
114138
```
115139

solution/0400-0499/0477.Total Hamming Distance/Solution.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
22
public:
33
int totalHammingDistance(vector<int>& nums) {
4-
int ans = 0;
5-
for (int i = 0; i < 31; ++i) {
6-
int a = 0, b = 0;
7-
for (int& v : nums) {
8-
int t = (v >> i) & 1;
9-
a += t;
10-
b += t ^ 1;
4+
int ans = 0, n = nums.size();
5+
for (int i = 0; i < 32; ++i) {
6+
int a = 0;
7+
for (int x : nums) {
8+
a += x >> i & 1;
119
}
10+
int b = n - a;
1211
ans += a * b;
1312
}
1413
return ans;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
func totalHammingDistance(nums []int) int {
2-
ans := 0
3-
for i := 0; i < 31; i++ {
4-
a, b := 0, 0
5-
for _, v := range nums {
6-
t := (v >> i) & 1
7-
a += t
8-
b += t ^ 1
1+
func totalHammingDistance(nums []int) (ans int) {
2+
for i := 0; i < 32; i++ {
3+
a := 0
4+
for _, x := range nums {
5+
a += x >> i & 1
96
}
7+
b := len(nums) - a
108
ans += a * b
119
}
12-
return ans
10+
return
1311
}

solution/0400-0499/0477.Total Hamming Distance/Solution.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
22
public int totalHammingDistance(int[] nums) {
3-
int ans = 0;
4-
for (int i = 0; i < 31; ++i) {
5-
int a = 0, b = 0;
6-
for (int v : nums) {
7-
int t = (v >> i) & 1;
8-
a += t;
9-
b += t ^ 1;
3+
int ans = 0, n = nums.length;
4+
for (int i = 0; i < 32; ++i) {
5+
int a = 0;
6+
for (int x : nums) {
7+
a += (x >> i & 1);
108
}
9+
int b = n - a;
1110
ans += a * b;
1211
}
1312
return ans;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
class Solution:
22
def totalHammingDistance(self, nums: List[int]) -> int:
3-
ans = 0
4-
for i in range(31):
5-
a = b = 0
6-
for v in nums:
7-
t = (v >> i) & 1
8-
if t:
9-
a += 1
10-
else:
11-
b += 1
3+
ans, n = 0, len(nums)
4+
for i in range(32):
5+
a = sum(x >> i & 1 for x in nums)
6+
b = n - a
127
ans += a * b
138
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
3+
let mut ans = 0;
4+
for i in 0..32 {
5+
let mut a = 0;
6+
for &x in nums.iter() {
7+
a += (x >> i) & 1;
8+
}
9+
let b = (nums.len() as i32) - a;
10+
ans += a * b;
11+
}
12+
ans
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function totalHammingDistance(nums: number[]): number {
2+
let ans = 0;
3+
for (let i = 0; i < 32; ++i) {
4+
const a = nums.filter(x => (x >> i) & 1).length;
5+
const b = nums.length - a;
6+
ans += a * b;
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)