Skip to content

feat: add solutions to lc problem: No.2997 #2204

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

Merged
merged 2 commits into from
Jan 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:位运算**

我们可以将数组 $nums$ 中的所有元素进行异或运算,判断得到的结果与 $k$ 的二进制表示中有多少位不同,这个数就是最少操作次数。

时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -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**
Expand All @@ -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);
}
}
```
Expand All @@ -98,15 +93,10 @@ class Solution {
class Solution {
public:
int minOperations(vector<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 __builtin_popcount(k);
}
};
```
Expand All @@ -115,30 +105,30 @@ 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))
}
```

### **TypeScript**

```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;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,31 @@ 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)$.

<!-- tabs:start -->

### **Python3**

```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**

```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);
}
}
```
Expand All @@ -88,15 +83,10 @@ class Solution {
class Solution {
public:
int minOperations(vector<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 __builtin_popcount(k);
}
};
```
Expand All @@ -105,30 +95,30 @@ 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))
}
```

### **TypeScript**

```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;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class Solution {
public:
int minOperations(vector<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 __builtin_popcount(k);
}
};
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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;
}