Skip to content

Commit

Permalink
Merge pull request #670 from 0xff-dev/779
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 779
  • Loading branch information
6boris authored Oct 28, 2023
2 parents e9042e4 + b64ce19 commit a6b668a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
37 changes: 24 additions & 13 deletions leetcode/701-800/0779.K-th-Symbol-in-Grammar/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# [779.K-th Symbol in Grammar][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
We build a table of `n` rows (**1-indexed**). We start by writing `0` in the 1<sup>st</sup> row. Now in every subsequent row, we look at the previous row and replace each occurrence of `0` with `01`, and each occurrence of `1` with `10`.

- For example, for `n = 3`, the 1<sup>st</sup> row is `0`, the 2<sup>nd</sup> row is `01`, and the 3<sup>rd</sup> row is `0110`.

Given two integer `n` and `k`, return the k<sup>th<sup> (**1-indexed**) symbol in the n<sup>th</sup> row of a table of `n` rows.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 1, k = 1
Output: 0
Explanation: row 1: 0
```

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
K-th Symbol in Grammar
```go
```
Input: n = 2, k = 1
Output: 0
Explanation:
row 1: 0
row 2: 01
```

**Example 3:**

```
Input: n = 2, k = 2
Output: 1
Explanation:
row 1: 0
row 2: 01
```

## 结语

Expand Down
33 changes: 31 additions & 2 deletions leetcode/701-800/0779.K-th-Symbol-in-Grammar/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package Solution

func Solution(x bool) bool {
return x
/*
0
0 1
0 1 1 0
0 1 1 0 1 0 0 1
1 2 3 4 5 6 7 8
*/
func Solution(n int, k int) int {
nn := n
path := make([]int, 0)
for nn > 0 {
path = append(path, k)
if k&1 == 1 {
k++
}
k /= 2
nn--
}
ans := 0
for i := len(path) - 2; i >= 0; i-- {
a, b := 0, 1
if ans != 0 {
a, b = 1, 0
}
if path[i] == path[i+1]*2 {
ans = b
continue
}
ans = a
}
return ans
}
21 changes: 11 additions & 10 deletions leetcode/701-800/0779.K-th-Symbol-in-Grammar/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n, k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 1, 0},
{"TestCase2", 2, 1, 0},
{"TestCase3", 2, 2, 1},
{"TestCase4", 28, 472, 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() {
}

0 comments on commit a6b668a

Please sign in to comment.