Skip to content

Commit fe13662

Browse files
authored
Merge pull request #1012 from 0xff-dev/1545
Add solution and test-cases for problem 1545
2 parents 57ee658 + 8396a0d commit fe13662

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [1545.Find Kth Bit in Nth Binary String][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 `n` and `k`, the binary string `Sn` is formed as follows:
5+
6+
- `S1 = "0"`
7+
- `Si = Si - 1 + "1" + reverse(invert(Si - 1))` for `i > 1`
8+
9+
Where `+` denotes the concatenation operation, `reverse(x)` returns the reversed string `x`, and `invert(x)` inverts all the bits in `x` (`0` changes to `1` and `1` changes to `0`).
10+
11+
For example, the first four strings in the above sequence are:
12+
13+
- `S1 = "0"`
14+
- `S2 = "011"`
15+
- `S3 = "0111001"`
16+
- `S4 = "011100110110001"`
17+
18+
Return the `kth` bit in `sn`. It is guaranteed that `k` is valid for the given `n`.
719

820
**Example 1:**
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
Input: n = 3, k = 1
24+
Output: "0"
25+
Explanation: S3 is "0111001".
26+
The 1st bit is "0".
1327
```
1428

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

20-
### 思路1
21-
> ...
22-
Find Kth Bit in Nth Binary String
23-
```go
2431
```
25-
32+
Input: n = 4, k = 11
33+
Output: "1"
34+
Explanation: S4 is "011100110110001".
35+
The 11th bit is "1".
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, k int) byte {
4+
alloc := 1
5+
for i := 2; i <= n; i++ {
6+
alloc = (alloc * 2) + 1
7+
}
8+
bs := make([]byte, alloc)
9+
bs[0] = '0'
10+
index := 1
11+
for i := 2; i <= n; i++ {
12+
bs[index] = '1'
13+
end := index
14+
index++
15+
for j := end - 1; j >= 0; j-- {
16+
bs[index] = '0'
17+
if bs[j] == '0' {
18+
bs[index] = '1'
19+
}
20+
index++
21+
}
22+
}
23+
return bs[k-1]
524
}

leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/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+
n, k int
14+
expect byte
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, 1, '0'},
17+
{"TestCase2", 4, 11, '1'},
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.n, c.k)
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.n, c.k)
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)