Skip to content

283. Move Zeroes#54

Open
dxxsxsxkx wants to merge 1 commit into22_generate_parenthesesfrom
283_move_zeroes
Open

283. Move Zeroes#54
dxxsxsxkx wants to merge 1 commit into22_generate_parenthesesfrom
283_move_zeroes

Conversation

@dxxsxsxkx
Copy link
Copy Markdown
Owner

public:
void moveZeroes(std::vector<int>& nums) {
// it - pos_last_nonzero?
auto it = std::remove(nums.begin(), nums.end(), 0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

仮に自分が面接官としてこの問題を出題するとしたら、 std::remove() と std::fill() の中身を書くようお願いすると思います。

@@ -0,0 +1,19 @@
#include <vector>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これが最適解かと思います。swapすると、どうせ上書きされるかもしれない場所に0を代入することになるので。swap内で、tmpへの代入もありますし。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この実装、とても分かりやすかったです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants