Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 283_move_zeroes/.DS_Store
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCodeとは関係ないですが、git管理する必要がないファイルは.gitignoreで指定しておくと良さそうです。

Binary file not shown.
1 change: 1 addition & 0 deletions 283_move_zeroes/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [283. Move Zeroes](https://leetcode.com/problems/move-zeroes/description/)
29 changes: 29 additions & 0 deletions 283_move_zeroes/step1.md
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

l,rは分かりづらいなと思いましたmm
渡しはleft, rightの場合は両端から中心に向かうポインタをイメージするので、この場合はfast, slowだとやっていることにイメージが近い感覚です。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
50 changes: 50 additions & 0 deletions 283_move_zeroes/step2.md
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lとrがどちらも0で初期化されると、leftとrightが最初は同じ位置にいることがやはり違和感があるかもしません><
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

non_zero_indexというよりは非ゼロの書き込み場所ぐらいの名前のほうが実態と合っている気がしました。
non_zero_indexで配列にアクセスしてゼロが得られることがあるのは少し違和感があります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)$
19 changes: 19 additions & 0 deletions 283_move_zeroes/step3.md
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

non_zero_indexのtypoですかね👀

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

PRを上げる前にLeetCodeをパスするか確認しておくと良さそうです。
テストを書くというアイデアもあると思います。

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.

ご指摘ありがとうございます。
こちらタイポです。正しくはzero_indexです。

```
1回目: 1分 9秒

2回目: 1分 2秒

3回目: 0分 56秒
Comment on lines +15 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この速度でコード書いているの素晴らしいですね。
逆にもう少しブラッシュアップする時間をとっても良さそうだと思いました。