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
9 changes: 9 additions & 0 deletions Arai60/44. Move Zeroes/Makefile
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)
26 changes: 26 additions & 0 deletions Arai60/44. Move Zeroes/main.cpp
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});
}
27 changes: 27 additions & 0 deletions Arai60/44. Move Zeroes/step1.hpp
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
69 changes: 69 additions & 0 deletions Arai60/44. Move Zeroes/step2.hpp
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がよいかな

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Owner Author

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として書くことが真っ先に思い浮かびそうですね。

std::fill(std::remove(nums.begin(), nums.end(), 0), nums.end(), 0);

*/

#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();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
24 changes: 24 additions & 0 deletions Arai60/44. Move Zeroes/step3.hpp
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