-
Notifications
You must be signed in to change notification settings - Fork 0
283. Move Zeroes #3
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
usatie
wants to merge
4
commits into
review-base
Choose a base branch
from
283/move-zeroes
base: review-base
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f24aaf1
Solved 283. Move Zeroes
usatie 89cb188
Modify files for each step to be header files in Arai60/44. Move Zeroes
usatie 0023c08
Add Makefile for Arai60/44. Move Zeroes
usatie 4498702
[283/move-zeroes] Remove using declaration in header files
usatie 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,9 @@ | ||
| NAME = a.out | ||
| SRC = main.cpp | ||
| CXX = g++ | ||
| CXXFLAGS = -Wall -Wextra -Werror -std=c++11 | ||
|
|
||
| all: $(NAME) | ||
|
|
||
| $(NAME): $(SRC) step3.hpp | ||
| $(CXX) -o $(NAME) $(SRC) $(CXXFLAGS) |
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,26 @@ | ||
| #include "step3.hpp" | ||
| #include <cassert> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| void test(std::initializer_list<int> input, | ||
| std::initializer_list<int> expected) { | ||
| std::vector<int> nums(input); | ||
| std::vector<int> expected_nums(expected); | ||
| std::cout << "input: "; | ||
| for (auto num : nums) { | ||
| std::cout << num << " "; | ||
| } | ||
| Solution().moveZeroes(nums); | ||
| // vectorに== operatorが使えるって知らなかった | ||
| // https://en.cppreference.com/w/cpp/container/vector/operator_cmp | ||
| assert(nums == expected_nums); | ||
| std::cout << " ===== OK =====" << std::endl; | ||
| } | ||
|
|
||
| int main() { | ||
| test({0, 1, 0, 3, 12}, {1, 3, 12, 0, 0}); | ||
| test({0}, {0}); | ||
| test({0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}); | ||
| test({1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}); | ||
| } |
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,27 @@ | ||
| /* | ||
| 何がわからなかったか | ||
| - N/A | ||
| 何を考えて解いていたか | ||
| 1. In place -> swapを使いたい | ||
| 2. Use two pointers, one for next insert position, the other for cursor | ||
| 3. When it == next_it, swap is not necessary but it's okay | ||
| 正解してから気づいたこと | ||
| - N/A | ||
| */ | ||
| #ifndef STEP1_HPP | ||
| #define STEP1_HPP | ||
| #include <algorithm> | ||
| #include <vector> | ||
| class Solution { | ||
| public: | ||
| void moveZeroes(std::vector<int> &nums) { | ||
| auto next_it = nums.begin(); | ||
| for (auto it = nums.begin(); it < nums.end(); ++it) { | ||
| if (*it != 0) { | ||
| std::iter_swap(next_it, it); | ||
| ++next_it; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| #endif |
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,69 @@ | ||
| /* | ||
| 講師陣はどのようなコメントを残すだろうか? | ||
| - | ||
| 他の人のコードを読んで考えたこと | ||
| - https://github.com/Yoshiki-Iwasa/Arai60/pull/59/files | ||
| 1. retainした後にresizeで0埋めするというのはコードとして見通しが良い | ||
| 2. &mut nums[1..]という書き方は、コピーにならないのか | ||
| ならないみたい | ||
| https://doc.rust-lang.org/book/ch04-03-slices.html | ||
| - https://github.com/fhiyo/leetcode/pull/54/files | ||
| 1. swapするのではなく, あとでまとめて0埋めする方がloop unrollingが効いて | ||
| 嬉しいこともあるというのはなるほど目から鱗だった | ||
| 改善する時にかんがえたこと | ||
| 1. `std::swap(*next_pos++, num)`とか`*next_pos++ = | ||
| num`という書き方もできるけど, error-proneなのでやめた | ||
| 2. Iteratorを使わない場合はswapしたいので、numがreferenceとなるよう注意 | ||
| 3. 0埋めをあとで行うのは, | ||
| filterしたい値が1つだけの場合に有効だが、複数の値をたとえば移動したい場合とかは順番もretainできるしswapがよいかな | ||
| */ | ||
|
|
||
| #ifndef STEP2_HPP | ||
| #define STEP2_HPP | ||
|
|
||
| #include <algorithm> | ||
| #include <vector> | ||
|
|
||
| class Solution { | ||
| public: | ||
| // 1. swapを使う | ||
| void moveZeroes1(std::vector<int> &nums) { | ||
| auto next_pos = nums.begin(); | ||
|
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. 変数名の単語はフルスペルで書いたほうが読みやすいと思うのですが、この規模であれば問題ないような気もしました。 |
||
| for (auto &num : nums) { | ||
| if (num != 0) { | ||
| std::swap(*next_pos, num); | ||
| ++next_pos; | ||
| } | ||
| } | ||
| } | ||
| // 1-2. swapを使う (with Iterator) | ||
| void moveZeroes12(vector<int> &nums) { | ||
| auto next_pos = nums.begin(); | ||
| for (auto it = nums.begin(); it != nums.end(); ++it) { | ||
| if (*it != 0) { | ||
| std::iter_swap(*next_pos, it); | ||
| ++next_pos; | ||
| } | ||
| } | ||
| } | ||
| // 2. swapを使わないで, 0埋めを後で行う | ||
| void moveZeroes2(vector<int> &nums) { | ||
| auto next_pos = nums.begin(); | ||
| for (auto &num : nums) { | ||
| if (num != 0) { | ||
| *next_pos = num; | ||
| ++next_pos; | ||
| } | ||
| } | ||
| std::fill(next_pos, nums.end(), 0); | ||
| } | ||
| // 2-2. for/if文をOne-linerっぽく書く (error-proneなので好きではない) | ||
| void moveZeroes22(vector<int> &nums) { | ||
| auto next_pos = nums.begin(); | ||
| for (auto num : nums) | ||
| if (num != 0) | ||
| *next_pos++ = num; | ||
| std::fill(next_pos, nums.end(), 0); | ||
| } | ||
| }; | ||
| #endif | ||
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,24 @@ | ||
| #ifndef STEP3_HPP | ||
| #define STEP3_HPP | ||
|
|
||
| #include <algorithm> | ||
| #include <vector> | ||
|
|
||
| /* | ||
| 3問連続で正解するのにかかった時間 2:57 | ||
| 時間計算量: O(N) | ||
| 空間計算量: O(1) | ||
| */ | ||
| class Solution { | ||
| public: | ||
| void moveZeroes(std::vector<int> &nums) { | ||
| auto next = nums.begin(); | ||
| for (auto it = nums.begin(); it != nums.end(); ++it) { | ||
| if (*it != 0) { | ||
| std::iter_swap(it, next); | ||
| ++next; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| #endif |
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.
いいと思います。
あとは、Erase-Remove Idiom というのがあることくらいですかね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.v62rdhwkdymb
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.
なるほどー。このwikiを読んでよく理解できました。これが常識なんですね。
https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
これを知っていたら、今回の例ならFill-Remove Idionとして書くことが真っ先に思い浮かびそうですね。