-
Notifications
You must be signed in to change notification settings - Fork 0
Move zeros #25
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: arai60
Are you sure you want to change the base?
Move zeros #25
Conversation
無駄なファイルはmerge先のbranchをremoteに反映してなかったことから生じていそうなので次からは無くなるはずです... |
nodchip
left a comment
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.
よいと思います。
| """ | ||
|
|
||
| # ループ不変式, nums[0], nums[1], nums[2], ..., nums[non_zero_index-1]までが0ではない | ||
| class Solution: |
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.
自分がこの問題を読んだとき、 C++ の標準ライブラリの std::remove() を連想しました。おそらく std::remove() を自力で書けるかどうかが主題なのではないかと思います。
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.
確かに似ています。ありがとうございます。
| """ | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
これちょっと名前が、nums[non_zero_index] != 0 に見えません?
実際に言いたいことは、non_zeros_length か count くらいでは。
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.
確かにこの名前だとnon_zero_indexが指しているindexの数は0でないときもあれば0の時もあるのでおかしいですね, 修正します
| class Solution { | ||
| public: | ||
| void moveZeroes(vector<int>& nums) { | ||
| auto result = nums.begin(); |
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.
resultというより、次にnon-zeroの値を入れるiterぐらいの意味ですかね。
| public: | ||
| void moveZeroes(vector<int>& nums) { | ||
| auto result = nums.begin(); | ||
| auto last = nums.end(); |
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.
変数を作らない方が分かりやすいと思います。
| void moveZeroes(vector<int>& nums) { | ||
| auto result = nums.begin(); | ||
| auto last = nums.end(); | ||
| int non_zero_length = 0; |
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.
distance(nums.begin(), result) == non_zero_lengthなので、不要な気がします。
| auto result = nums.begin(); | ||
| auto last = nums.end(); | ||
| int non_zero_length = 0; | ||
| for (auto first = nums.begin();first != last; first++){ |
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.
一つずつ要素を見ているだけなので、firstよりitの方が分かりやすい気がします。ここでは値を順番に見ているだけなので、イテレータを使わなくて良さそうです。
for (int num : nums) { ... }| auto last = nums.end(); | ||
| int non_zero_length = 0; | ||
| for (auto first = nums.begin();first != last; first++){ | ||
| if (!(*first == 0)){ |
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.
普通に*first != 0で良さそうです。
| int non_zero_length = 0; | ||
| for (auto first = nums.begin();first != last; first++){ | ||
| if (!(*first == 0)){ | ||
| *result++ = std::move(*first); |
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.
intでstd::move()をしても意味は無さそうです。
| non_zero_length++; | ||
| } | ||
| } | ||
| for (; non_zero_length < nums.size(); non_zero_length++){ |
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.
fill(result, nums.end(), 0);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.
C++に詳しくないのでここまでのレビューとても参考になりました。ありがとうございます
問題
283. Move Zeroes