-
Notifications
You must be signed in to change notification settings - Fork 0
779. K-th Symbol in Grammar #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
int kthGrammar(int n, int k) { | ||
if (k == 1) return 0; | ||
int k_parity = k & 1; | ||
if (k_parity == 0) k_parity = 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この行が読みやすさに寄与しているか微妙だなと思い、取り除いてk_parity が 0 か 1 で良い気がしました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そう思います。ちょっと1-indexedにとらわれすぎました。
if (k_parity == 0) k_parity = 2; | ||
int k_prev = k + 1 >> 1; | ||
int prev = kthGrammar(n - 1, k_prev); | ||
if (prev == 0 && k_parity == 1 || prev == 1 && k_parity == 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k_parity == 2 の時には prev とは違う文字になるの方が伝わりやすいと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
反転するという捉え方が書いているときになかったのでこんな感じになってしまいました。
ただその捉え方をするなら結局while文などで書いたほうがいい気もしますね。
if (k == 1) return 0; | ||
int k_parity = k & 1; | ||
if (k_parity == 0) k_parity = 2; | ||
int k_prev = k + 1 >> 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+
と >>
のどちらの優先度が高いか、分かりづらいように思いました。
int k_prev = (k + 1) >> 1;
https://leetcode.com/problems/k-th-symbol-in-grammar/description/