diff --git a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/README.md b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/README.md index 5bd2ac3a7..fdf436eae 100755 --- a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/README.md +++ b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/README.md @@ -1,28 +1,39 @@ # [1545.Find Kth Bit in Nth Binary String][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 `n` and `k`, the binary string `Sn` is formed as follows: + +- `S1 = "0"` +- `Si = Si - 1 + "1" + reverse(invert(Si - 1))` for `i > 1` + +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`). + +For example, the first four strings in the above sequence are: + +- `S1 = "0"` +- `S2 = "011"` +- `S3 = "0111001"` +- `S4 = "011100110110001"` + +Return the `kth` bit in `sn`. It is guaranteed that `k` is valid for the given `n`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 3, k = 1 +Output: "0" +Explanation: S3 is "0111001". +The 1st bit is "0". ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find Kth Bit in Nth Binary String -```go ``` - +Input: n = 4, k = 11 +Output: "1" +Explanation: S4 is "011100110110001". +The 11th bit is "1". +``` ## 结语 diff --git a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution.go b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution.go index d115ccf5e..8405128bb 100644 --- a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution.go +++ b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, k int) byte { + alloc := 1 + for i := 2; i <= n; i++ { + alloc = (alloc * 2) + 1 + } + bs := make([]byte, alloc) + bs[0] = '0' + index := 1 + for i := 2; i <= n; i++ { + bs[index] = '1' + end := index + index++ + for j := end - 1; j >= 0; j-- { + bs[index] = '0' + if bs[j] == '0' { + bs[index] = '1' + } + index++ + } + } + return bs[k-1] } diff --git a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution_test.go b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution_test.go index 14ff50eb4..8fb8301bd 100644 --- a/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution_test.go +++ b/leetcode/1501-1600/1545.Find-Kth-Bit-in-Nth-Binary-String/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n, k int + expect byte }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 3, 1, '0'}, + {"TestCase2", 4, 11, '1'}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.k) 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.n, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }