Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions 283.MoveZeroes/fill_zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int last_zero_index = 0;
for (int i = 0; i < nums.size(); i++) {
Copy link

Choose a reason for hiding this comment

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

型の違いや、暗黙の型変換が行われいてることを意識しているか?という点が少しだけ気になります。
当然に読んでいるかもしれませんが、もしまだでしたらこれらの項目を一読しておくことをお勧めいたします。
暗記する必要はないと思いますが、気になった時に調べられるようにというのと、気にする癖をつけるためです。
https://en.cppreference.com/w/cpp/language/const_cast
https://en.cppreference.com/w/cpp/language/static_cast
https://en.cppreference.com/w/cpp/language/dynamic_cast
https://en.cppreference.com/w/cpp/language/reinterpret_cast
https://en.cppreference.com/w/cpp/language/implicit_conversion
https://en.cppreference.com/w/cpp/language/explicit_cast

Copy link

Choose a reason for hiding this comment

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

int については私はそんなにうるさくはないですが、size_t が unsigned integer であるというのは注意です。
for (int i = 0; i < num.size() - 1; ++i) という回し方をすると、num.size() が0を返したときにアンダーフローして止まらないことがあります。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@usatie @oda
レビュー頂きありがとうございます。
どこでキャストが行われているのか分かりやすくするよう可能な限り明示しようぐらいの感覚でした。
この辺りのドキュメント再読いたします。

num.size() が0を返したときにアンダーフローして止まらないことがあります。

これまで意識しておりませんでした。
他の問題でfor (int i = 0; i < num.size() - 1; ++i) このような条件式は使っていたと思いますが気付けませんでした。int型にキャストするのかループに渡す前にチェックするなど意識しておこうと思います。

Copy link

Choose a reason for hiding this comment

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

あ、すみません。これをアンダーフローと呼ぶのは本来の用語としては誤りのようです。

アンダーフローは、float などで値が0に近くなりすぎて表現できなくなることで、signed integer が負に大きくなることもオーバーフローというようです。なお、unsigned の場合は module 2^n で考えているのでオーバーフローでもないとするのが本来の用語みたいです。

if (nums[i] == 0) {
continue;
}
nums[last_zero_index] = nums[i];
last_zero_index++;
}

for (int i = last_zero_index; i < nums.size(); i++) {
nums[i] = 0;
}
}
};
50 changes: 50 additions & 0 deletions 283.MoveZeroes/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## ステップ1
といたことがあって解法を覚えていた。
forループのindexでnums全体を探索しながら
non_zero_indexを使って0でない要素を前に詰めていく

## ステップ2
fill_zero.cpp
usatieの解法を参考に実装。イテレータは使わない方法
https://github.com/usatie/leetcode/pull/3/commits/f24aaf1c13c4b2fc1c6d935297d7a318e5b82dda

remove.cpp
Yoshiki-Iwasaの解法を参考に実装。
vectorのeraseではなか、<algorithm>のremoveを使う
前者だとforの中で使うのでO(n^2)となるが、後者だと舐めながら0を消して前に詰めるのでO(n)
removeは削除するのではなく、不要な要素を後ろに寄せているだけなので
詰め終わった箇所からnums.end()までゴミデータ?が残っているのは違和感
Copy link

Choose a reason for hiding this comment

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

他の方のPRにも目を通していただいていたようなので読んでいると思いますが、Erase-Remove Idiom というのがあることくらいですかね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.v62rdhwkdymb


## ステップ3
**3回書き直しやりましょう、といっているのは、不自然なところや負荷の高いところは覚えられないからです。**

## 他の方の解法
・イテレータを使っている
イテレータなんとなく苦手意識がある。使えるメソッドを覚えられていないので馴染みを持てていないからか
・寄せてから0で埋めることもできる。なるほどです。
・c++のテストの書き方やファイルの分け方勉強になります。
https://github.com/usatie/leetcode/pull/3/commits/f24aaf1c13c4b2fc1c6d935297d7a318e5b82dda

・0の場合は削除して配列を前に詰める
この方法もなるほどです。
調べたらc++も同じことできそう。
https://github.com/Yoshiki-Iwasa/Arai60/pull/59

・基本の解法は同じか
https://github.com/fhiyo/leetcode/pull/54

>zero_indexにしてしまうと、客観的に(or 後から自分で見た場合)自然言語の直感と反するんですね。
https://github.com/Mike0121/LeetCode/pull/24

>「はじめの0が出てくるまでは、i か i + 1 を指していて、0が出てくると、
>そこで止まり、swap する関係>上、そこから先は常にはじめの0を指している。」
>くらいじゃないですか?だから、and first_zero < len(nums)がなくても動きますよね。
https://github.com/fhiyo/leetcode/pull/18#discussion_r1629642641

ここの変数名めちゃくちゃ迷いました。
>zero_indexだとzeroが入っているindexなのかなと思ってしまいます。多分上のコードを見る感じだとnon_zero_lengthぐらいでしょうか
こう見える人もいるのでよく考えてつけないと。zero_indexでいけそうと一瞬思ってします。
自分はnon_zero_indexにしました。

## Discorなど

8 changes: 8 additions & 0 deletions 283.MoveZeroes/remove.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
// 0を削除してしまった後の最後のindex
auto end_index = remove(nums.begin(), nums.end(), 0);
fill(end_index, nums.end(), 0);
}
};
12 changes: 12 additions & 0 deletions 283.MoveZeroes/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int non_zero_index = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
swap(nums[non_zero_index], nums[i]);
non_zero_index++;
}
}
}
};
13 changes: 13 additions & 0 deletions 283.MoveZeroes/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int non_zero_index = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 0) {
continue;
}
swap(nums[non_zero_index], nums[i]);
non_zero_index++;
}
}
};
13 changes: 13 additions & 0 deletions 283.MoveZeroes/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int non_zero_index = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 0) {
continue;
}
swap(nums[non_zero_index], nums[i]);
non_zero_index++;
}
}
};