From 1e65cff7e53e15695e2d5c524489be02060527d1 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Thu, 24 Nov 2022 17:21:25 +0800 Subject: [PATCH] Remove elements --- include/s0283_move_zeroes.hpp | 13 +++++++++++++ notes/src/SUMMARY.md | 1 + notes/src/remove_elements.md | 11 +++++++++++ src/s0027_remove_element.cpp | 20 ++++++++------------ src/s0283_move_zeroes.cpp | 20 ++++++++++++++++++++ tests/s0283_move_zeroes.cpp | 19 +++++++++++++++++++ 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 include/s0283_move_zeroes.hpp create mode 100644 notes/src/remove_elements.md create mode 100644 src/s0283_move_zeroes.cpp create mode 100644 tests/s0283_move_zeroes.cpp diff --git a/include/s0283_move_zeroes.hpp b/include/s0283_move_zeroes.hpp new file mode 100644 index 0000000..ce0a36d --- /dev/null +++ b/include/s0283_move_zeroes.hpp @@ -0,0 +1,13 @@ +#ifndef S0283_MOVE_ZEROES_HPP +#define S0283_MOVE_ZEROES_HPP + +#include + +using namespace std; + +class Solution { + public: + void moveZeroes(vector& nums); +}; + +#endif diff --git a/notes/src/SUMMARY.md b/notes/src/SUMMARY.md index 26fba9d..50acb15 100644 --- a/notes/src/SUMMARY.md +++ b/notes/src/SUMMARY.md @@ -1,6 +1,7 @@ # 数组 - [二分查找](./bin_search.md) +- [移除元素](./remove_elements.md) # 经典代码 diff --git a/notes/src/remove_elements.md b/notes/src/remove_elements.md new file mode 100644 index 0000000..90f6c7a --- /dev/null +++ b/notes/src/remove_elements.md @@ -0,0 +1,11 @@ +# 移除元素 + +查找数组中的目标元素 target ,原地移除该元素,返回移除后的数组长度。 + +双指针法,快指针用于查找新数组的元素,即不是 target 的元素,慢指针用于覆盖,即写入新数组的元素。 + +## 相关题目 + +- [27. 移除元素](https://leetcode.com/problems/remove-element/) +- [24. 删除排序数组中的重复项](https://leetcode.com/problems/swap-nodes-in-pairs/) +- [283. 移动零](https://leetcode.com/problems/move-zeroes/) diff --git a/src/s0027_remove_element.cpp b/src/s0027_remove_element.cpp index 046a457..71d475a 100644 --- a/src/s0027_remove_element.cpp +++ b/src/s0027_remove_element.cpp @@ -1,18 +1,14 @@ #include "s0027_remove_element.hpp" int Solution::removeElement(vector& nums, int val) { - int size = nums.size(); - if (size == 0) { - return 0; - } - int fast{0}, slow{0}; - for (; fast < size; ++fast) { - if (nums.at(fast) == val) { - continue; - } else { - nums[slow] = nums.at(fast); - ++slow; + int len = nums.size(); + int f{0}, s{0}; + while (f < len) { + if (nums[f] != val) { + nums[s] = nums[f]; + ++s; } + ++f; } - return slow; + return s; } diff --git a/src/s0283_move_zeroes.cpp b/src/s0283_move_zeroes.cpp new file mode 100644 index 0000000..3f58eef --- /dev/null +++ b/src/s0283_move_zeroes.cpp @@ -0,0 +1,20 @@ +#include "s0283_move_zeroes.hpp" + +void Solution::moveZeroes(vector& nums) { + int len = nums.size(); + if (len <= 1) { + return; + } + int s{0}, f{0}; + while (f < len) { + if (nums[f] != 0) { + nums[s] = nums[f]; + ++s; + } + ++f; + } + while (s < len) { + nums[s] = 0; + ++s; + } +} diff --git a/tests/s0283_move_zeroes.cpp b/tests/s0283_move_zeroes.cpp new file mode 100644 index 0000000..346e0ec --- /dev/null +++ b/tests/s0283_move_zeroes.cpp @@ -0,0 +1,19 @@ +#include "s0283_move_zeroes.hpp" + +#include + +TEST(Problem283, Case1) { + vector i{0, 1, 0, 3, 12}; + vector o{1, 3, 12, 0, 0}; + Solution solution; + solution.moveZeroes(i); + EXPECT_EQ(i, o); +} + +TEST(Problem283, Case2) { + vector i{0}; + vector o{0}; + Solution solution; + solution.moveZeroes(i); + EXPECT_EQ(i, o); +}