-
Notifications
You must be signed in to change notification settings - Fork 0
283. Move Zeroes #22
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?
283. Move Zeroes #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## 問題: [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/description/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Step 1 | ||
|
|
||
| - 0を探すポインタと0以外を探すポインタを使う | ||
| - 0に遭遇したら、そのインデックスの1つ先から0以外を探す | ||
| - 0以外を見つけたら、その値と前述で見つけた0を入れ替える | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def moveZeroes(self, nums: List[int]) -> None: | ||
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| l, r = 0, 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l,rは分かりづらいなと思いましたmm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ただ、もう少し意味のある名前のほうがbetterな気がするので、zero_i, nonzero_iなどなんらか位置関係以外の意味を持たせたいです。 |
||
| while l < len(nums) and r < len(nums): | ||
| if nums[l] != 0: | ||
| l += 1 | ||
| continue | ||
| r = l + 1 | ||
| while r < len(nums): | ||
| if nums[r] == 0: | ||
| r += 1 | ||
| else: | ||
| nums[l], nums[r] = nums[r], nums[l] | ||
| break | ||
| ``` | ||
|
|
||
| 時間計算量: O(n^2) | ||
|
|
||
| 空間計算量: O(1) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Step 2 | ||
|
|
||
| - Step 1のコードは遅い | ||
| - rのインデックスを毎回lの1個先にリセットしないようにしたい | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def moveZeroes(self, nums: List[int]) -> None: | ||
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| l, r = 0, 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lとrがどちらも0で初期化されると、leftとrightが最初は同じ位置にいることがやはり違和感があるかもしません>< |
||
| while l < len(nums) and r < len(nums): | ||
| if nums[l] != 0: | ||
| l += 1 | ||
| r = max(r, l) | ||
| continue | ||
| while r < len(nums): | ||
| if nums[r] == 0: | ||
| r += 1 | ||
| else: | ||
| nums[l], nums[r] = nums[r], nums[l] | ||
| break | ||
|
|
||
| ``` | ||
|
|
||
| 時間計算量: $O(n)$ | ||
|
|
||
| 空間計算量: $O(1)$ | ||
|
|
||
| - 他の人のコード見たらもっとスマートなのがあった | ||
| - 0でない値が次にどのインデックスに来るか変数で持つ | ||
| - 変数は0で始まり、0でない値がみつかったら、変数が示すインデックスに格納されている値と入れ替える | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def moveZeroes(self, nums: List[int]) -> None: | ||
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| non_zero_index = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non_zero_indexというよりは非ゼロの書き込み場所ぐらいの名前のほうが実態と合っている気がしました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 | ||
|
|
||
| ``` | ||
| 時間計算量: $O(n)$ | ||
|
|
||
| 空間計算量: $O(1)$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def moveZeroes(self, nums: List[int]) -> None: | ||
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| non_zero_index = 0 | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non_zero_indexのtypoですかね👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PRを上げる前にLeetCodeをパスするか確認しておくと良さそうです。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ご指摘ありがとうございます。 |
||
| ``` | ||
| 1回目: 1分 9秒 | ||
|
|
||
| 2回目: 1分 2秒 | ||
|
|
||
| 3回目: 0分 56秒 | ||
|
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この速度でコード書いているの素晴らしいですね。 |
||
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.
LeetCodeとは関係ないですが、git管理する必要がないファイルは.gitignoreで指定しておくと良さそうです。