Open
Conversation
nodchip
reviewed
Apr 22, 2026
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
|
|
||
| def skip_index(index, zero_skip=True): |
There was a problem hiding this comment.
自分なら skip_zeros と skip_non_zeros に分けるのですが、趣味の範囲だと思います。
Owner
Author
There was a problem hiding this comment.
その点は迷ったのですが、処理がほぼ同じなので今回はまとめました。
関数を分ける選択をされる方がいることは参考になります。
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| last_non_zero_index = 0 |
There was a problem hiding this comment.
ものすごく細かい話ですが、これを-1にして、ループ内で+=1してから処理をするか、それとも0にして処理後に+=1にするか、は二分探索の式の意味を決めるのと似たような感覚があるなと思いました。
変数名をこれにするなら、-1にする方が妥当かもと個人的に思いますが、まあどちらも普通ですね。
私の感覚だと0の場合はnext_non_zero_indexですが、これはこれで分かりにくいかもしれません。あるいはnon_zero_countですが、indexに使う意味がわかりにくいかもしれません。
Owner
Author
There was a problem hiding this comment.
二分探索で閉区間か開区間かを考えるのに似ている、ということですかね。たしかに last か next か解釈は分かれそうです。今回は半開区間に対応していそうで、私の意図はこれだったのでそのままにしておきます。
first_zero_index というか、私の感覚は、ゼロが削除された文字列の長さですね。
というコメントもありましたので変数名に count を用いるのも一理あるな、と思いました
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/move-zeroes/description/?show=1