Skip to content
Open
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
98 changes: 98 additions & 0 deletions 283. Move Zeroes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 進め方

Step1 : 問題を解く。

Step2 : 他の人のPRを参照し、コメントする。

Step3 : 3回続けてエラーが出ないように書く。ドキュメントを参照する。

# 実践

## Step1

### 思考ログ

計算量は時間はO(n^2) 空間はO(1) 

10^8 / 10 ^ 6 = 100秒くらい?

コピーを作らずに時間計算量を小さくする方法が思いつかないので書く

```python
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 1
while i >= 0:
if nums[i] != 0:
i -= 1
continue
nums.append(0)
nums.pop(i)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これでi番目がpopされるの知らなかったです

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.

私も初めて使いました。O(N)かかるので注意です。
https://wiki.python.org/moin/TimeComplexity

i -= 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.

個人的にはfor文でreversedで回す方が好みです

return nums
```

## Step2

### 同じ問題を解いた人のプルリクを見る

https://github.com/olsen-blue/Arai60/pull/55/files

- けっこう解法があるっぽい

> 何も書いていない except は ^C で送られてくるシグナルさえ捕まえます。
タイミングよく ^C を送ることで、括弧の対応関係を変えられるプログラム、セキュリティー的にもまずそうではないですか。
>
- けっこう書いてました。地雷を埋め込んでました。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

まあ、そういう状況で使うことは起きないし、起きたとしたら全部書き換えるのだという覚悟が決まった上でやっているならばいいんですが、広い意味で「遠い帰結に想像がいくか」ということかなと思います。


https://github.com/hroc135/leetcode/pull/51/files

- こちらは私のと似ている
- これは同意。YAGNIでしょうか。仕事でも自分の昔の実装に邪魔されることがありました。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

YAGNI 知らなかったです。勉強になります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What to look for in a code review にも同じことが書かれていますね。

A particular type of complexity is over-engineering, where developers have made the code more generic than it needs to be, or added functionality that isn’t presently needed by the system.

https://google.github.io/eng-practices/review/reviewer/looking-for.html#complexity


> 個人的には、達成したい目的に依存しない複雑性の排除のほうが保守性には大切かなーと思いました「もしかしたら役立つかも」という想定は大抵実現しないので、目的を達成するために選択できる手段の中から最もシンプルなものを選ぶほうが保守性は高くなると思います
>
- https://ja.wikipedia.org/wiki/YAGNI
- Python にも連結リストのライブラリがありそう
- https://pypi.org/project/llist/

これが計算量的にも、処理の分かりやすさもベストな感じがしました。

```python
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
not_zero_i = 0
for i in range(len(nums)):
if nums[i] == 0:
continue
nums[not_zero_i] = nums[i]
not_zero_i += 1
for i in range(not_zero_i, len(nums)):
nums[i] = 0
return nums
```

not_zero_iが二度目のループでも使えるのが気持よいですね。

## Step3

### 3回連続で再現

```python
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
non_zero_index = 0
for i in range(len(nums)):
if nums[i] == 0:
continue
nums[non_zero_index] = nums[i]
non_zero_index += 1
for i in range(non_zero_index, len(nums)):
nums[i] = 0
return nums
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

シグネチャで None を返すことになっているので、この行は不要ですね。

```

やっぱ綺麗ですね。