Skip to content
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

Add solution and test-cases for problem 2429 #1095

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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() {
}
Loading