Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e5531d2

Browse files
Update README.md
1 parent 6240b5e commit e5531d2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5538,6 +5538,15 @@ class Solution:
55385538
l1.next = self.mergeTwoLists(l1.next, l2)
55395539
return l1 or l2
55405540
```
5541+
#### [779. 第K个语法符号](https://leetcode-cn.com/problems/k-th-symbol-in-grammar/comments/)
5542+
```python
5543+
class Solution:
5544+
def kthGrammar(self, N: int, K: int) -> int:
5545+
return 0 if N == 1 else (K + 1) % 2 ^ self.kthGrammar(N - 1, (K + 1) // 2);
5546+
```
5547+
- 由于递推关系式可知第 `N` 行的第 `K` 个数计算自第 `N - 1` 行的第 `(K + 1) // 2` 个数
5548+
- 我们称第 `N` 行的第 `K` 个数为C,第 `N - 1` 行的第 `(K + 1) // 2` 个数为 P
5549+
- `0 → 01`,`1 → 10`,可见变换后实际上是在原来的数字后加了相对的数(这里称 0 与 1 相对),那么如果 K 为奇数则 `C = P`,否则 `C = P 的相对数`
55415550

55425551
# 常用技巧总结
55435552
- set 中的 in 操作时间复杂度为 O(1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /