Conversation
無駄なファイルはmerge先のbranchをremoteに反映してなかったことから生じていそうなので次からは無くなるはずです... |
| """ | ||
|
|
||
| # ループ不変式, nums[0], nums[1], nums[2], ..., nums[non_zero_index-1]までが0ではない | ||
| class Solution: |
There was a problem hiding this comment.
自分がこの問題を読んだとき、 C++ の標準ライブラリの std::remove() を連想しました。おそらく std::remove() を自力で書けるかどうかが主題なのではないかと思います。
| """ | ||
| Do not return anything, modify nums in-place instead. | ||
| """ | ||
| non_zero_index = 0 |
There was a problem hiding this comment.
これちょっと名前が、nums[non_zero_index] != 0 に見えません?
実際に言いたいことは、non_zeros_length か count くらいでは。
There was a problem hiding this comment.
確かにこの名前だと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.
resultというより、次にnon-zeroの値を入れるiterぐらいの意味ですかね。
| public: | ||
| void moveZeroes(vector<int>& nums) { | ||
| auto result = nums.begin(); | ||
| auto last = nums.end(); |
| 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.
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.
一つずつ要素を見ているだけなので、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)){ |
| int non_zero_length = 0; | ||
| for (auto first = nums.begin();first != last; first++){ | ||
| if (!(*first == 0)){ | ||
| *result++ = std::move(*first); |
| non_zero_length++; | ||
| } | ||
| } | ||
| for (; non_zero_length < nums.size(); non_zero_length++){ |
There was a problem hiding this comment.
C++に詳しくないのでここまでのレビューとても参考になりました。ありがとうございます
問題
283. Move Zeroes