779 kth symbol in grammar#47
Open
kitano-kazuki wants to merge 3 commits into
Open
Conversation
Yuto729
reviewed
May 10, 2026
Comment on lines
+3
to
+7
| if n <= 0: | ||
| raise ValueError("n must be > 0") | ||
|
|
||
| if k > 2 ** (n - 1): | ||
| raise ValueError("k must be less than the number of symbols in the row") |
|
|
||
| is_reverse = False | ||
| row = n | ||
| col = k |
There was a problem hiding this comment.
rowに対してcolということだと思いますが、問題文にcolの定義が出てきているわけではないので、ここは単純にkの方がわかりやすい気がします
Owner
Author
There was a problem hiding this comment.
引数で渡されるkと今回注目するもの(変動する値)を区別したかったんですよね.
colだと確かに問題の定義にないのでわかりにくそうというのは同意です
Comment on lines
+12
to
+21
| while row > 1: | ||
| if col % 2 == 0: | ||
| is_reverse = not is_reverse | ||
| row = row - 1 | ||
| col = (col + 1) // 2 | ||
|
|
||
| if is_reverse: | ||
| return 1 | ||
| else: | ||
| return 0 |
There was a problem hiding this comment.
ループ内で累積した結果をそのまま返すというやり方もありそうです。少しトリッキーになりますね
result = 0
row = n
col = k
while row > 1:
if col % 2 == 0:
# 反転する
result = 1 - result
row = row - 1
col = (col + 1) // 2
return result|
LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/k-th-symbol-in-grammar/description/