283. Move Zeroes#22
Conversation
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| l, r = 0, 1 |
There was a problem hiding this comment.
l,rは分かりづらいなと思いましたmm
渡しはleft, rightの場合は両端から中心に向かうポインタをイメージするので、この場合はfast, slowだとやっていることにイメージが近い感覚です。
There was a problem hiding this comment.
ただ、もう少し意味のある名前のほうがbetterな気がするので、zero_i, nonzero_iなどなんらか位置関係以外の意味を持たせたいです。
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| l, r = 0, 0 |
There was a problem hiding this comment.
lとrがどちらも0で初期化されると、leftとrightが最初は同じ位置にいることがやはり違和感があるかもしません><
left, rightで定義しているなら別の場所からスタートする存在であってほしい気持ちがあります。
| for i in range(len(nums)): | ||
| if nums[i] != 0: | ||
| nums[i], nums[non_zero_index] = nums[non_zero_index], nums[i] | ||
| zero_index += 1 |
There was a problem hiding this comment.
PRを上げる前にLeetCodeをパスするか確認しておくと良さそうです。
テストを書くというアイデアもあると思います。
There was a problem hiding this comment.
ご指摘ありがとうございます。
こちらタイポです。正しくはzero_indexです。
| 1回目: 1分 9秒 | ||
|
|
||
| 2回目: 1分 2秒 | ||
|
|
||
| 3回目: 0分 56秒 |
There was a problem hiding this comment.
この速度でコード書いているの素晴らしいですね。
逆にもう少しブラッシュアップする時間をとっても良さそうだと思いました。
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| non_zero_index = 0 |
There was a problem hiding this comment.
non_zero_indexというよりは非ゼロの書き込み場所ぐらいの名前のほうが実態と合っている気がしました。
non_zero_indexで配列にアクセスしてゼロが得られることがあるのは少し違和感があります。
There was a problem hiding this comment.
There was a problem hiding this comment.
LeetCodeとは関係ないですが、git管理する必要がないファイルは.gitignoreで指定しておくと良さそうです。
This problem: 283. Move Zeroes
Next problem: 252. Meeting Rooms