From abab9deaa4019529a94f91f32c17015fa267c5ce Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 8 May 2024 12:58:59 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0477 No.0477.Total Hamming Distance --- .../0477.Total Hamming Distance/README.md | 88 +++++++++++------- .../0477.Total Hamming Distance/README_EN.md | 90 ++++++++++++------- .../0477.Total Hamming Distance/Solution.cpp | 13 ++- .../0477.Total Hamming Distance/Solution.go | 16 ++-- .../0477.Total Hamming Distance/Solution.java | 13 ++- .../0477.Total Hamming Distance/Solution.py | 13 +-- .../0477.Total Hamming Distance/Solution.rs | 14 +++ .../0477.Total Hamming Distance/Solution.ts | 9 ++ 8 files changed, 159 insertions(+), 97 deletions(-) create mode 100644 solution/0400-0499/0477.Total Hamming Distance/Solution.rs create mode 100644 solution/0400-0499/0477.Total Hamming Distance/Solution.ts diff --git a/solution/0400-0499/0477.Total Hamming Distance/README.md b/solution/0400-0499/0477.Total Hamming Distance/README.md index f1db0ca99a936..081340847599e 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/README.md +++ b/solution/0400-0499/0477.Total Hamming Distance/README.md @@ -45,20 +45,19 @@ 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)$。 + ```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 ``` @@ -66,14 +65,13 @@ class Solution: ```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; @@ -85,14 +83,13 @@ class Solution { class Solution { public: int totalHammingDistance(vector& 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; @@ -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 { + 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 + } } ``` diff --git a/solution/0400-0499/0477.Total Hamming Distance/README_EN.md b/solution/0400-0499/0477.Total Hamming Distance/README_EN.md index 5fc2444b0410f..dad9b59f53084 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/README_EN.md +++ b/solution/0400-0499/0477.Total Hamming Distance/README_EN.md @@ -40,22 +40,21 @@ 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)$. ```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 ``` @@ -63,14 +62,13 @@ class Solution: ```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; @@ -82,14 +80,13 @@ class Solution { class Solution { public: int totalHammingDistance(vector& 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; @@ -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 { + 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 + } } ``` diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp b/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp index 5a0c07ffec55e..8a564079bc28f 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp @@ -1,14 +1,13 @@ class Solution { public: int totalHammingDistance(vector& 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; diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.go b/solution/0400-0499/0477.Total Hamming Distance/Solution.go index e1c2eb679982c..1f5332d56c960 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/Solution.go +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.go @@ -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 } \ No newline at end of file diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.java b/solution/0400-0499/0477.Total Hamming Distance/Solution.java index cfc3413d57d33..bc0e98ed53ea6 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/Solution.java +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.java @@ -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; diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.py b/solution/0400-0499/0477.Total Hamming Distance/Solution.py index 22d5e732ca403..e9aa7b4f43d77 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/Solution.py +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.py @@ -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 diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.rs b/solution/0400-0499/0477.Total Hamming Distance/Solution.rs new file mode 100644 index 0000000000000..4c476ad37ecef --- /dev/null +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.rs @@ -0,0 +1,14 @@ +impl Solution { + pub fn total_hamming_distance(nums: Vec) -> 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 + } +} diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.ts b/solution/0400-0499/0477.Total Hamming Distance/Solution.ts new file mode 100644 index 0000000000000..ca5f897b285f9 --- /dev/null +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.ts @@ -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; +}