Skip to content

Commit d40356b

Browse files
committed
Add solution and test-cases for problem 2220
1 parent 1fbf15a commit d40356b

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

leetcode/2201-2300/2220.Minimum-Bit-Flips-to-Convert-Number/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2220.Minimum Bit Flips to Convert Number][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+
A **bit flip** of a number `x` is choosing a bit in the binary representation of `x` and **flipping** it from either `0` to `1` or `1` to `0`.
5+
6+
- For example, for `x = 7`, the binary representation is `111` and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get `110`, flip the second bit from the right to get `101`, flip the fifth bit from the right (a leading zero) to get `10111`, etc.
7+
8+
Given two integers `start` and `goal`, return the **minimum** number of **bit flips** to convert `start` to `goal`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: start = 10, goal = 7
14+
Output: 3
15+
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
16+
- Flip the first bit from the right: 1010 -> 1011.
17+
- Flip the third bit from the right: 1011 -> 1111.
18+
- Flip the fourth bit from the right: 1111 -> 0111.
19+
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Bit Flips to Convert Number
23-
```go
2424
```
25-
25+
Input: start = 3, goal = 4
26+
Output: 3
27+
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
28+
- Flip the first bit from the right: 011 -> 010.
29+
- Flip the second bit from the right: 010 -> 000.
30+
- Flip the third bit from the right: 000 -> 100.
31+
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(start int, goal int) int {
4+
x := start ^ goal
5+
n := 0
6+
for x > 0 {
7+
n++
8+
x = x & (x - 1)
9+
}
10+
return n
511
}

leetcode/2201-2300/2220.Minimum-Bit-Flips-to-Convert-Number/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
start, goal int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 10, 7, 3},
17+
{"TestCase2", 3, 4, 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.start, c.goal)
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.start, c.goal)
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)