Skip to content

Commit

Permalink
Add solution and test-cases for problem 2429
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jan 15, 2025
1 parent d569e0c commit c91f7ed
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
36 changes: 22 additions & 14 deletions leetcode/2401-2500/2429.Minimize-XOR/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [2429.Minimize XOR][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given two positive integers `num1` and `num2`, find the positive integer `x` such that:

- `x` has the same number of set bits as `num2`, and
- The value `x XOR num1` is minimal.

Note that `XOR` is the bitwise XOR operation.

Return the integer `x`. The test cases are generated such that `x` is **uniquely determined**.

The number of **set bits** of an integer is the number of `1`'s in its binary representation.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: num1 = 3, num2 = 5
Output: 3
Explanation:
The binary representations of num1 and num2 are 0011 and 0101, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 3 = 0 is minimal.
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Minimize XOR
```go
```

Input: num1 = 1, num2 = 12
Output: 3
Explanation:
The binary representations of num1 and num2 are 0001 and 1100, respectively.
The integer 3 has the same number of set bits as num2, and the value 3 XOR 1 = 2 is minimal.
```

## 结语

Expand Down
39 changes: 37 additions & 2 deletions leetcode/2401-2500/2429.Minimize-XOR/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
package Solution

func Solution(x bool) bool {
return x
func countOfOne(x int) int {
c := 0
for x > 0 {
c++
x = x & (x - 1)
}
return c
}

func Solution(num1 int, num2 int) int {
n1, n2 := countOfOne(num1), countOfOne(num2)
if n1 == n2 {
return num1
}
if n1 > n2 {
diff := n1 - n2
mask := 1
for i := 0; i < 32 && diff > 0; i++ {
if mask&num1 == mask {
diff--
}
mask <<= 1
}
return num1 & ^(mask - 1)
}
ans := num1
mask := 1
diff := n2 - n1

for i := 0; i < 32 && diff > 0; i++ {
if mask&num1 == 0 {
ans |= mask
diff--
}
mask <<= 1
}
return ans
}
19 changes: 9 additions & 10 deletions leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n1, n2 int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, 5, 3},
{"TestCase2", 1, 12, 3},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n1, c.n2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n1, c.n2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit c91f7ed

Please sign in to comment.