Skip to content

283. Move Zeroes#51

Open
tom4649 wants to merge 1 commit intomainfrom
283.Move-Zeroes
Open

283. Move Zeroes#51
tom4649 wants to merge 1 commit intomainfrom
283.Move-Zeroes

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Apr 22, 2026

Comment thread 283.Move-Zeroes/sol2.py
Do not return anything, modify nums in-place instead.
"""

def skip_index(index, zero_skip=True):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分なら skip_zeros と skip_non_zeros に分けるのですが、趣味の範囲だと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

その点は迷ったのですが、処理がほぼ同じなので今回はまとめました。
関数を分ける選択をされる方がいることは参考になります。

Comment thread 283.Move-Zeroes/sol3.py
"""
Do not return anything, modify nums in-place instead.
"""
last_non_zero_index = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ものすごく細かい話ですが、これを-1にして、ループ内で+=1してから処理をするか、それとも0にして処理後に+=1にするか、は二分探索の式の意味を決めるのと似たような感覚があるなと思いました。
変数名をこれにするなら、-1にする方が妥当かもと個人的に思いますが、まあどちらも普通ですね。
私の感覚だと0の場合はnext_non_zero_indexですが、これはこれで分かりにくいかもしれません。あるいはnon_zero_countですが、indexに使う意味がわかりにくいかもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

二分探索で閉区間か開区間かを考えるのに似ている、ということですかね。たしかに last か next か解釈は分かれそうです。今回は半開区間に対応していそうで、私の意図はこれだったのでそのままにしておきます。

shining-ai/leetcode#54

first_zero_index というか、私の感覚は、ゼロが削除された文字列の長さですね。

というコメントもありましたので変数名に count を用いるのも一理あるな、と思いました

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants