-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0283-move-zeroes.md #55
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?
Conversation
| return nothing, modify nums in-place instead. | ||
| """ | ||
| def swap(index1, index2): | ||
| nums[index1], nums[index2] = nums[index2], nums[index1] |
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.
自分はしませんでしたが、このように swap 処理を関数に切り出す方が for 文の中の見通しがよさそうだと感じました。好みかもしれませんが。
| swap_target_index = 0 | ||
| for i in range(len(nums)): | ||
| if nums[i] != 0: | ||
| swap(i, swap_target_index) |
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.
ただの感想になりますが、swap_target_index <= i なので swap(swap_target_index, i) と書くのも自然なのかなと少し思いました。ただどちらが自然なのか、それとも意識しなくてもいいのか、わかりませんでした。
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が主役で、swap_target_indexが脇役のように感じたのでこの順序を採用しましたが、自分もどちらがより自然かわからなかったです。
| - [コメント集](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.v62rdhwkdymb) | ||
| - https://discord.com/channels/1084280443945353267/1235829049511903273/1276579345099526267 | ||
| - > まとめて 0 fill は、loop unrolling できたりするのでちょっと嬉しいこともあるでしょう。 | ||
| - https://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%BC%E3%83%97%E5%B1%95%E9%96%8B |
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.
キーワードからいろいろ調べられていて素晴らしいと思いました。自分は loop unrolling を初めて聞いたので調べてみようと思います。
この問題:https://leetcode.com/problems/move-zeroes/
次の問題:https://leetcode.com/problems/meeting-rooms/