Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0477 #2760

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 56 additions & 32 deletions solution/0400-0499/0477.Total Hamming Distance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,33 @@ HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2

### 方法一:位运算

我们在 $[0, 31]$ 的范围内枚举每一位,对于当前枚举的位 $i$,我们统计所有数字中的第 $i$ 位为 $1$ 的个数 $a$,那么这些数字中的第 $i$ 位为 $0$ 的个数就是 $b = n - a$,其中 $n$ 是数组的长度。这样的话,在第 $i$ 位上的汉明距离之和就是 $a \times b$,我们把所有的位的汉明距离相加即为答案。

时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组的长度和数组中的元素的最大值。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
ans = 0
for i in range(31):
a = b = 0
for v in nums:
t = (v >> i) & 1
if t:
a += 1
else:
b += 1
ans, n = 0, len(nums)
for i in range(32):
a = sum(x >> i & 1 for x in nums)
b = n - a
ans += a * b
return ans
```

```java
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += (x >> i & 1);
}
int b = n - a;
ans += a * b;
}
return ans;
Expand All @@ -85,14 +83,13 @@ class Solution {
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int& v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.size();
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += x >> i & 1;
}
int b = n - a;
ans += a * b;
}
return ans;
Expand All @@ -101,18 +98,45 @@ public:
```

```go
func totalHammingDistance(nums []int) int {
ans := 0
for i := 0; i < 31; i++ {
a, b := 0, 0
for _, v := range nums {
t := (v >> i) & 1
a += t
b += t ^ 1
func totalHammingDistance(nums []int) (ans int) {
for i := 0; i < 32; i++ {
a := 0
for _, x := range nums {
a += x >> i & 1
}
b := len(nums) - a
ans += a * b
}
return ans
return
}
```

```ts
function totalHammingDistance(nums: number[]): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const a = nums.filter(x => (x >> i) & 1).length;
const b = nums.length - a;
ans += a * b;
}
return ans;
}
```

```rust
impl Solution {
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 0..32 {
let mut a = 0;
for &x in nums.iter() {
a += (x >> i) & 1;
}
let b = (nums.len() as i32) - a;
ans += a * b;
}
ans
}
}
```

Expand Down
90 changes: 57 additions & 33 deletions solution/0400-0499/0477.Total Hamming Distance/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,35 @@ HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2

## Solutions

### Solution 1
### Solution 1: Bit Manipulation

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.

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)$.

<!-- tabs:start -->

```python
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
ans = 0
for i in range(31):
a = b = 0
for v in nums:
t = (v >> i) & 1
if t:
a += 1
else:
b += 1
ans, n = 0, len(nums)
for i in range(32):
a = sum(x >> i & 1 for x in nums)
b = n - a
ans += a * b
return ans
```

```java
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += (x >> i & 1);
}
int b = n - a;
ans += a * b;
}
return ans;
Expand All @@ -82,14 +80,13 @@ class Solution {
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int& v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.size();
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += x >> i & 1;
}
int b = n - a;
ans += a * b;
}
return ans;
Expand All @@ -98,18 +95,45 @@ public:
```

```go
func totalHammingDistance(nums []int) int {
ans := 0
for i := 0; i < 31; i++ {
a, b := 0, 0
for _, v := range nums {
t := (v >> i) & 1
a += t
b += t ^ 1
func totalHammingDistance(nums []int) (ans int) {
for i := 0; i < 32; i++ {
a := 0
for _, x := range nums {
a += x >> i & 1
}
b := len(nums) - a
ans += a * b
}
return ans
return
}
```

```ts
function totalHammingDistance(nums: number[]): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const a = nums.filter(x => (x >> i) & 1).length;
const b = nums.length - a;
ans += a * b;
}
return ans;
}
```

```rust
impl Solution {
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 0..32 {
let mut a = 0;
for &x in nums.iter() {
a += (x >> i) & 1;
}
let b = (nums.len() as i32) - a;
ans += a * b;
}
ans
}
}
```

Expand Down
13 changes: 6 additions & 7 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int& v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.size();
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += x >> i & 1;
}
int b = n - a;
ans += a * b;
}
return ans;
Expand Down
16 changes: 7 additions & 9 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
func totalHammingDistance(nums []int) int {
ans := 0
for i := 0; i < 31; i++ {
a, b := 0, 0
for _, v := range nums {
t := (v >> i) & 1
a += t
b += t ^ 1
func totalHammingDistance(nums []int) (ans int) {
for i := 0; i < 32; i++ {
a := 0
for _, x := range nums {
a += x >> i & 1
}
b := len(nums) - a
ans += a * b
}
return ans
return
}
13 changes: 6 additions & 7 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0;
for (int i = 0; i < 31; ++i) {
int a = 0, b = 0;
for (int v : nums) {
int t = (v >> i) & 1;
a += t;
b += t ^ 1;
int ans = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += (x >> i & 1);
}
int b = n - a;
ans += a * b;
}
return ans;
Expand Down
13 changes: 4 additions & 9 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
ans = 0
for i in range(31):
a = b = 0
for v in nums:
t = (v >> i) & 1
if t:
a += 1
else:
b += 1
ans, n = 0, len(nums)
for i in range(32):
a = sum(x >> i & 1 for x in nums)
b = n - a
ans += a * b
return ans
14 changes: 14 additions & 0 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl Solution {
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 0..32 {
let mut a = 0;
for &x in nums.iter() {
a += (x >> i) & 1;
}
let b = (nums.len() as i32) - a;
ans += a * b;
}
ans
}
}
9 changes: 9 additions & 0 deletions solution/0400-0499/0477.Total Hamming Distance/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function totalHammingDistance(nums: number[]): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const a = nums.filter(x => (x >> i) & 1).length;
const b = nums.length - a;
ans += a * b;
}
return ans;
}
Loading