Skip to content

Commit 3e3be6c

Browse files
committed
二刷27
1 parent 77f401d commit 3e3be6c

File tree

4 files changed

+92
-35
lines changed

4 files changed

+92
-35
lines changed

docs/0027-remove-element.adoc

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,89 @@
11
[#0027-remove-element]
2-
= 27. Remove Element
2+
= 27. 移除元素
33

4-
{leetcode}/problems/remove-element/[LeetCode - Remove Element^]
4+
https://leetcode.cn/problems/remove-element/[LeetCode - 27. 移除元素 ^]
55

6-
Given an array _nums_ and a value _val_, remove all instances of that value https://en.wikipedia.org/wiki/In-place_algorithm[*in-place*^] and return the new length.
6+
给你一个数组 `nums` 和一个值 `val`,你需要 *https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地]* 移除所有数值等于 `val` 的元素。元素的顺序可能发生改变。然后返回 `nums` 中与 `val` 不同的元素的数量。
77

8-
Do not allocate extra space for another array, you must do this by *modifying the input array https://en.wikipedia.org/wiki/In-place_algorithm[in-place^]* with O(1) extra memory.
8+
假设 `nums` 中不等于 `val` 的元素数量为 `k`,要通过此题,您需要执行以下操作:
99

10-
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
10+
* 更改 `nums` 数组,使 `nums` 的前 `k` 个元素包含不等于 `val` 的元素。`nums` 的其余元素和 `nums` 的大小并不重要。* 返回 `k`
1111
12-
*Example 1:*
12+
*用户评测:*
1313

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
Given _nums_ = *[3,2,2,3]*, _val_ = *3*,
17-
18-
Your function should return length = *2*, with the first two elements of _nums_ being *2*.
14+
评测机将使用以下代码测试您的解决方案:
1915

20-
It doesn't matter what you leave beyond the returned length.
16+
....
17+
int[] nums = [...]; // 输入数组
18+
int val = ...; // 要移除的值
19+
int[] expectedNums = [...]; // 长度正确的预期答案。
20+
// 它以不等于 val 的值排序。
2121
22-
----
22+
int k = removeElement(nums, val); // 调用你的实现
2323
24-
*Example 2:*
24+
assert k == expectedNums.length;
25+
sort(nums, 0, k); // 排序 nums 的前 k 个元素
26+
for (int i = 0; i < actualLength; i++) {
27+
assert nums[i] == expectedNums[i];
28+
}
29+
....
2530

26-
[subs="verbatim,quotes,macros"]
27-
----
28-
Given _nums_ = *[0,1,2,2,3,0,4,2]*, _val_ = *2*,
31+
如果所有的断言都通过,你的解决方案将会 *通过*
2932

30-
Your function should return length = *`5`*, with the first five elements of _`nums`_ containing *`0`*, *`1`*, *`3`*, *`0`*, and *4*.
3133

32-
Note that the order of those five elements can be arbitrary.
34+
*示例 1:*
3335

34-
It doesn't matter what values are set beyond the returned length.
35-
----
36+
....
37+
输入:nums = [3,2,2,3], val = 3
38+
输出:2, nums = [2,2,_,_]
39+
解释:你的函数函数应该返回 k = 2, 并且 nums 中的前两个元素均为 2。
40+
你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。
41+
....
3642

37-
*Clarification:*
43+
*示例 2:*
3844

39-
Confused why the returned value is an integer but your answer is an array?
45+
....
46+
输入:nums = [0,1,2,2,3,0,4,2], val = 2
47+
输出:5, nums = [0,1,4,0,3,_,_,_]
48+
解释:你的函数应该返回 k = 5,并且 nums 中的前五个元素为 0,0,1,3,4。
49+
注意这五个元素可以任意顺序返回。
50+
你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。
51+
....
4052

41-
Note that the input array is passed in by *reference*, which means modification to the input array will be known to the caller as well.
53+
*提示:*
4254

43-
Internally you can think of this:
55+
* `+0 <= nums.length <= 100+`
56+
* `+0 <= nums[i] <= 50+`
57+
* `+0 <= val <= 100+`
4458
45-
[subs="verbatim,quotes,macros"]
46-
----
47-
// *nums* is passed in by reference. (i.e., without making a copy)
48-
int len = removeElement(nums, val);
4959
50-
// any modification to *nums* in your function would be known by the caller.
51-
// using the length returned by your function, it prints the first *len* elements.
52-
for (int i = 0; i < len; i++) {
53-
print(nums[i]);
54-
}
55-
----
60+
== 思路分析
5661

62+
快慢指针。慢指针记录满足条件的长度,快指针指向需要处理的元素。如果 `nums[fast] == val`,则只移动 `fast`;否则,`nums[slow++] == nums[fast++]`。
5763

5864
[[src-0027]]
65+
[tabs]
66+
====
67+
一刷::
68+
+
69+
--
5970
[{java_src_attr}]
6071
----
6172
include::{sourcedir}/_0027_RemoveElement.java[tag=answer]
6273
----
74+
--
75+
76+
二刷::
77+
+
78+
--
79+
[{java_src_attr}]
80+
----
81+
include::{sourcedir}/_0027_RemoveElement_2.java[tag=answer]
82+
----
83+
--
84+
====
85+
86+
87+
== 参考资料
88+
6389

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ endif::[]
363363
|{doc_base_url}/0011-container-with-most-water.adoc[题解]
364364
|✅ 双指针
365365

366+
|{counter:codes}
367+
|{leetcode_base_url}/remove-element/[27. 移除元素^]
368+
|{doc_base_url}/0027-remove-element.adoc[题解]
369+
|✅ 快慢指针。慢指针记录满足条件的长度,快指针指向需要处理的元素。
370+
366371
|===
367372

368373
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0027_RemoveElement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
*/
6363
public class _0027_RemoveElement {
6464
// tag::answer[]
65+
66+
/**
67+
* @author D瓜哥 · https://www.diguage.com
68+
* @since 2018-07-19 18:55
69+
*/
6570
public static int removeElement(int[] nums, int val) {
6671
if (nums == null || nums.length == 0) {
6772
return 0;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0027_RemoveElement_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-20 18:25:23
8+
*/
9+
public static int removeElement(int[] nums, int val) {
10+
int slow = 0, fast = 0;
11+
while (fast < nums.length) {
12+
if (nums[fast] == val) {
13+
fast++;
14+
}else {
15+
nums[slow++] = nums[fast++];
16+
}
17+
}
18+
return slow;
19+
}
20+
// end::answer[]
21+
}

0 commit comments

Comments
 (0)