diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README.md b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README.md index 16eee48b1c799..50922874c84ed 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README.md +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README.md @@ -54,6 +54,12 @@ +**方法一:位运算** + +我们可以将数组 $nums$ 中的所有元素进行异或运算,判断得到的结果与 $k$ 的二进制表示中有多少位不同,这个数就是最少操作次数。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -63,13 +69,7 @@ ```python class Solution: def minOperations(self, nums: List[int], k: int) -> int: - ans = 0 - for i in range(20): - v = 0 - for x in nums: - v ^= x >> i & 1 - ans += (k >> i & 1) != v - return ans + return reduce(xor, nums, k).bit_count() ``` ### **Java** @@ -79,15 +79,10 @@ class Solution: ```java class Solution { public int minOperations(int[] nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return Integer.bitCount(k); } } ``` @@ -98,15 +93,10 @@ class Solution { class Solution { public: int minOperations(vector& nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return __builtin_popcount(k); } }; ``` @@ -115,14 +105,10 @@ public: ```go func minOperations(nums []int, k int) (ans int) { - for i := 0; i < 20; i++ { - v := 0 - for _, x := range nums { - v ^= x >> i & 1 - } - ans += k>>i&1 ^ v + for _, x := range nums { + k ^= x } - return + return bits.OnesCount(uint(k)) } ``` @@ -130,15 +116,19 @@ func minOperations(nums []int, k int) (ans int) { ```ts function minOperations(nums: number[], k: number): number { - let ans = 0; - for (let i = 0; i < 20; ++i) { - let v = 0; - for (const x of nums) { - v ^= (x >> i) & 1; - } - ans += ((k >> i) & 1) ^ v; + for (const x of nums) { + k ^= x; } - return ans; + return bitCount(k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; } ``` diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md index b6cd68926d95d..95dc5f870480c 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/README_EN.md @@ -48,6 +48,12 @@ It can be shown that we cannot make the XOR equal to k in less than 2 operations ## Solutions +**Solution 1: Bit Manipulation** + +We can perform a bitwise XOR operation on all elements in the array $nums$. The number of bits that differ from the binary representation of $k$ in the result is the minimum number of operations. + +The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$. + ### **Python3** @@ -55,13 +61,7 @@ It can be shown that we cannot make the XOR equal to k in less than 2 operations ```python class Solution: def minOperations(self, nums: List[int], k: int) -> int: - ans = 0 - for i in range(20): - v = 0 - for x in nums: - v ^= x >> i & 1 - ans += (k >> i & 1) != v - return ans + return reduce(xor, nums, k).bit_count() ``` ### **Java** @@ -69,15 +69,10 @@ class Solution: ```java class Solution { public int minOperations(int[] nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return Integer.bitCount(k); } } ``` @@ -88,15 +83,10 @@ class Solution { class Solution { public: int minOperations(vector& nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return __builtin_popcount(k); } }; ``` @@ -105,14 +95,10 @@ public: ```go func minOperations(nums []int, k int) (ans int) { - for i := 0; i < 20; i++ { - v := 0 - for _, x := range nums { - v ^= x >> i & 1 - } - ans += k>>i&1 ^ v + for _, x := range nums { + k ^= x } - return + return bits.OnesCount(uint(k)) } ``` @@ -120,15 +106,19 @@ func minOperations(nums []int, k int) (ans int) { ```ts function minOperations(nums: number[], k: number): number { - let ans = 0; - for (let i = 0; i < 20; ++i) { - let v = 0; - for (const x of nums) { - v ^= (x >> i) & 1; - } - ans += ((k >> i) & 1) ^ v; + for (const x of nums) { + k ^= x; } - return ans; + return bitCount(k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; } ``` diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp index 7717cfa06fc7d..8cadef54c5f03 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.cpp @@ -1,14 +1,9 @@ class Solution { public: int minOperations(vector& nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return __builtin_popcount(k); } }; \ No newline at end of file diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go index a8c6b5278ac66..d49a1464db503 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.go @@ -1,10 +1,6 @@ func minOperations(nums []int, k int) (ans int) { - for i := 0; i < 20; i++ { - v := 0 - for _, x := range nums { - v ^= x >> i & 1 - } - ans += k>>i&1 ^ v + for _, x := range nums { + k ^= x } - return + return bits.OnesCount(uint(k)) } \ No newline at end of file diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java index 5c13d4f76094b..b323c766f4809 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.java @@ -1,13 +1,8 @@ class Solution { public int minOperations(int[] nums, int k) { - int ans = 0; - for (int i = 0; i < 20; ++i) { - int v = 0; - for (int x : nums) { - v ^= (x >> i & 1); - } - ans += k >> i & 1 ^ v; + for (int x : nums) { + k ^= x; } - return ans; + return Integer.bitCount(k); } } \ No newline at end of file diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py index 92fdc83805505..11249c04f9d45 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.py @@ -1,9 +1,3 @@ class Solution: def minOperations(self, nums: List[int], k: int) -> int: - ans = 0 - for i in range(20): - v = 0 - for x in nums: - v ^= x >> i & 1 - ans += (k >> i & 1) != v - return ans + return reduce(xor, nums, k).bit_count() diff --git a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts index 142deff264ddb..aff57f254606c 100644 --- a/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts +++ b/solution/2900-2999/2997.Minimum Number of Operations to Make Array XOR Equal to K/Solution.ts @@ -1,11 +1,15 @@ function minOperations(nums: number[], k: number): number { - let ans = 0; - for (let i = 0; i < 20; ++i) { - let v = 0; - for (const x of nums) { - v ^= (x >> i) & 1; - } - ans += ((k >> i) & 1) ^ v; + for (const x of nums) { + k ^= x; } - return ans; + return bitCount(k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; }