|
1 | 1 | [#0027-remove-element]
|
2 |
| -= 27. Remove Element |
| 2 | += 27. 移除元素 |
3 | 3 |
|
4 |
| -{leetcode}/problems/remove-element/[LeetCode - Remove Element^] |
| 4 | +https://leetcode.cn/problems/remove-element/[LeetCode - 27. 移除元素 ^] |
5 | 5 |
|
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` 不同的元素的数量。 |
7 | 7 |
|
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`,要通过此题,您需要执行以下操作: |
9 | 9 |
|
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`。 |
11 | 11 |
|
12 |
| -*Example 1:* |
| 12 | +*用户评测:* |
13 | 13 |
|
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 | +评测机将使用以下代码测试您的解决方案: |
19 | 15 |
|
20 |
| -It doesn't matter what you leave beyond the returned length. |
| 16 | +.... |
| 17 | +int[] nums = [...]; // 输入数组 |
| 18 | +int val = ...; // 要移除的值 |
| 19 | +int[] expectedNums = [...]; // 长度正确的预期答案。 |
| 20 | + // 它以不等于 val 的值排序。 |
21 | 21 |
|
22 |
| ----- |
| 22 | +int k = removeElement(nums, val); // 调用你的实现 |
23 | 23 |
|
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 | +.... |
25 | 30 |
|
26 |
| -[subs="verbatim,quotes,macros"] |
27 |
| ----- |
28 |
| -Given _nums_ = *[0,1,2,2,3,0,4,2]*, _val_ = *2*, |
| 31 | +如果所有的断言都通过,你的解决方案将会 *通过*。 |
29 | 32 |
|
30 |
| -Your function should return length = *`5`*, with the first five elements of _`nums`_ containing *`0`*, *`1`*, *`3`*, *`0`*, and *4*. |
31 | 33 |
|
32 |
| -Note that the order of those five elements can be arbitrary. |
| 34 | +*示例 1:* |
33 | 35 |
|
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 | +.... |
36 | 42 |
|
37 |
| -*Clarification:* |
| 43 | +*示例 2:* |
38 | 44 |
|
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 | +.... |
40 | 52 |
|
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 | +*提示:* |
42 | 54 |
|
43 |
| -Internally you can think of this: |
| 55 | +* `+0 <= nums.length <= 100+` |
| 56 | +* `+0 <= nums[i] <= 50+` |
| 57 | +* `+0 <= val <= 100+` |
44 | 58 |
|
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); |
49 | 59 |
|
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 | +== 思路分析 |
56 | 61 |
|
| 62 | +快慢指针。慢指针记录满足条件的长度,快指针指向需要处理的元素。如果 `nums[fast] == val`,则只移动 `fast`;否则,`nums[slow++] == nums[fast++]`。 |
57 | 63 |
|
58 | 64 | [[src-0027]]
|
| 65 | +[tabs] |
| 66 | +==== |
| 67 | +一刷:: |
| 68 | ++ |
| 69 | +-- |
59 | 70 | [{java_src_attr}]
|
60 | 71 | ----
|
61 | 72 | include::{sourcedir}/_0027_RemoveElement.java[tag=answer]
|
62 | 73 | ----
|
| 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 | + |
63 | 89 |
|
0 commit comments