From c91f7ed445470cf4015438ee293942764c59fb4f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 15 Jan 2025 09:46:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 2429 --- .../2401-2500/2429.Minimize-XOR/README.md | 36 ++++++++++------- .../2401-2500/2429.Minimize-XOR/Solution.go | 39 ++++++++++++++++++- .../2429.Minimize-XOR/Solution_test.go | 19 +++++---- 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/leetcode/2401-2500/2429.Minimize-XOR/README.md b/leetcode/2401-2500/2429.Minimize-XOR/README.md index 4debd8e0d..84bde4275 100755 --- a/leetcode/2401-2500/2429.Minimize-XOR/README.md +++ b/leetcode/2401-2500/2429.Minimize-XOR/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/2401-2500/2429.Minimize-XOR/Solution.go b/leetcode/2401-2500/2429.Minimize-XOR/Solution.go index d115ccf5e..026dae64f 100644 --- a/leetcode/2401-2500/2429.Minimize-XOR/Solution.go +++ b/leetcode/2401-2500/2429.Minimize-XOR/Solution.go @@ -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 } diff --git a/leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go b/leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go index 14ff50eb4..998297343 100644 --- a/leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go +++ b/leetcode/2401-2500/2429.Minimize-XOR/Solution_test.go @@ -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() { }