Skip to content

Commit c91f7ed

Browse files
committed
Add solution and test-cases for problem 2429
1 parent d569e0c commit c91f7ed

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

leetcode/2401-2500/2429.Minimize-XOR/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [2429.Minimize XOR][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given two positive integers `num1` and `num2`, find the positive integer `x` such that:
5+
6+
- `x` has the same number of set bits as `num2`, and
7+
- The value `x XOR num1` is minimal.
8+
9+
Note that `XOR` is the bitwise XOR operation.
10+
11+
Return the integer `x`. The test cases are generated such that `x` is **uniquely determined**.
12+
13+
The number of **set bits** of an integer is the number of `1`'s in its binary representation.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: num1 = 3, num2 = 5
19+
Output: 3
20+
Explanation:
21+
The binary representations of num1 and num2 are 0011 and 0101, respectively.
22+
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
1323
```
1424

15-
## 题意
16-
> ...
17-
18-
## 题解
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Minimize XOR
23-
```go
2427
```
25-
28+
Input: num1 = 1, num2 = 12
29+
Output: 3
30+
Explanation:
31+
The binary representations of num1 and num2 are 0001 and 1100, respectively.
32+
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
33+
```
2634

2735
## 结语
2836

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func countOfOne(x int) int {
4+
c := 0
5+
for x > 0 {
6+
c++
7+
x = x & (x - 1)
8+
}
9+
return c
10+
}
11+
12+
func Solution(num1 int, num2 int) int {
13+
n1, n2 := countOfOne(num1), countOfOne(num2)
14+
if n1 == n2 {
15+
return num1
16+
}
17+
if n1 > n2 {
18+
diff := n1 - n2
19+
mask := 1
20+
for i := 0; i < 32 && diff > 0; i++ {
21+
if mask&num1 == mask {
22+
diff--
23+
}
24+
mask <<= 1
25+
}
26+
return num1 & ^(mask - 1)
27+
}
28+
ans := num1
29+
mask := 1
30+
diff := n2 - n1
31+
32+
for i := 0; i < 32 && diff > 0; i++ {
33+
if mask&num1 == 0 {
34+
ans |= mask
35+
diff--
36+
}
37+
mask <<= 1
38+
}
39+
return ans
540
}

leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n1, n2 int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, 5, 3},
17+
{"TestCase2", 1, 12, 3},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n1, c.n2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n1, c.n2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)