-
Notifications
You must be signed in to change notification settings - Fork 0
Create 283. Move Zeroes.md #53
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?
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,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) | ||
| i -= 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. 個人的にはfor文でreversedで回す方が好みです |
||
| return nums | ||
| ``` | ||
|
|
||
| ## Step2 | ||
|
|
||
| ### 同じ問題を解いた人のプルリクを見る | ||
|
|
||
| https://github.com/olsen-blue/Arai60/pull/55/files | ||
|
|
||
| - けっこう解法があるっぽい | ||
|
|
||
| > 何も書いていない except は ^C で送られてくるシグナルさえ捕まえます。 | ||
| タイミングよく ^C を送ることで、括弧の対応関係を変えられるプログラム、セキュリティー的にもまずそうではないですか。 | ||
| > | ||
| - けっこう書いてました。地雷を埋め込んでました。 | ||
|
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. まあ、そういう状況で使うことは起きないし、起きたとしたら全部書き換えるのだという覚悟が決まった上でやっているならばいいんですが、広い意味で「遠い帰結に想像がいくか」ということかなと思います。 |
||
|
|
||
| https://github.com/hroc135/leetcode/pull/51/files | ||
|
|
||
| - こちらは私のと似ている | ||
| - これは同意。YAGNIでしょうか。仕事でも自分の昔の実装に邪魔されることがありました。 | ||
|
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. YAGNI 知らなかったです。勉強になります。 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. What to look for in a code review にも同じことが書かれていますね。
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 | ||
|
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. シグネチャで None を返すことになっているので、この行は不要ですね。 |
||
| ``` | ||
|
|
||
| やっぱ綺麗ですね。 | ||
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.
これでi番目がpopされるの知らなかったです
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.
私も初めて使いました。O(N)かかるので注意です。
https://wiki.python.org/moin/TimeComplexity