-
Notifications
You must be signed in to change notification settings - Fork 0
283. Move Zeroes #59
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
Open
Ryotaro25
wants to merge
1
commit into
main
Choose a base branch
from
problem54
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
283. Move Zeroes #59
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++) { | ||
| 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; | ||
| } | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()までゴミデータ?が残っているのは違和感 | ||
|
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. 他の方のPRにも目を通していただいていたようなので読んでいると思いますが、Erase-Remove Idiom というのがあることくらいですかね。 |
||
|
|
||
| ## ステップ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など | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | ||
| } | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | ||
| } | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
型の違いや、暗黙の型変換が行われいてることを意識しているか?という点が少しだけ気になります。
当然に読んでいるかもしれませんが、もしまだでしたらこれらの項目を一読しておくことをお勧めいたします。
暗記する必要はないと思いますが、気になった時に調べられるようにというのと、気にする癖をつけるためです。
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
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 については私はそんなにうるさくはないですが、size_t が unsigned integer であるというのは注意です。
for (int i = 0; i < num.size() - 1; ++i) という回し方をすると、num.size() が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.
@usatie @oda
レビュー頂きありがとうございます。
どこでキャストが行われているのか分かりやすくするよう可能な限り明示しようぐらいの感覚でした。
この辺りのドキュメント再読いたします。
これまで意識しておりませんでした。
他の問題でfor (int i = 0; i < num.size() - 1; ++i) このような条件式は使っていたと思いますが気付けませんでした。int型にキャストするのかループに渡す前にチェックするなど意識しておこうと思います。
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.
あ、すみません。これをアンダーフローと呼ぶのは本来の用語としては誤りのようです。
アンダーフローは、float などで値が0に近くなりすぎて表現できなくなることで、signed integer が負に大きくなることもオーバーフローというようです。なお、unsigned の場合は module 2^n で考えているのでオーバーフローでもないとするのが本来の用語みたいです。