|
1 | 1 | [#0026-remove-duplicates-from-sorted-array]
|
2 |
| -= 26. Remove Duplicates from Sorted Array |
| 2 | += 26. 删除有序数组中的重复项 |
3 | 3 |
|
4 |
| -{leetcode}/problems/remove-duplicates-from-sorted-array/[LeetCode - Remove Duplicates from Sorted Array^] |
| 4 | +https://leetcode.cn/problems/remove-duplicates-from-sorted-array/[LeetCode - 26. 删除有序数组中的重复项 ^] |
5 | 5 |
|
6 |
| -Given a sorted array _nums_, remove the duplicates https://en.wikipedia.org/wiki/In-place_algorithm[*in-place*^] such that each element appear only _once_ and return the new length. |
| 6 | +给你一个 *非严格递增排列* 的数组 `nums` ,请你 *http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地]* 删除重复出现的元素,使每个元素 *只出现一次*,返回删除后数组的新长度。元素的 *相对顺序* 应该保持 *一致* 。然后返回 `nums` 中唯一元素的个数。 |
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` 的唯一元素的数量为 `k`,你需要做以下事情确保你的题解可以被通过: |
9 | 9 |
|
10 |
| -*Example 1:* |
| 10 | +* 更改数组 `nums` ,使 `nums` 的前 `k` 个元素包含唯一元素,并按照它们最初在 `nums` 中出现的顺序排列。`nums` 的其余元素与 `nums` 的大小不重要。 |
| 11 | +* 返回 `k` 。 |
11 | 12 |
|
12 |
| -[subs="verbatim,quotes,macros"] |
13 |
| ----- |
14 |
| -Given _nums_ = *[1,1,2]*, |
| 13 | +*判题标准:* |
15 | 14 |
|
16 |
| -Your function should return length = *`2`*, with the first two elements of _`nums`_ being *`1`* and *`2`* respectively. |
| 15 | +系统会用下面的代码来测试你的题解: |
17 | 16 |
|
18 |
| -It doesn't matter what you leave beyond the returned length. |
19 |
| ----- |
| 17 | +.... |
| 18 | +int[] nums = [...]; // 输入数组 |
| 19 | +int[] expectedNums = [...]; // 长度正确的期望答案 |
20 | 20 |
|
21 |
| -*Example 2:* |
| 21 | +int k = removeDuplicates(nums); // 调用 |
22 | 22 |
|
23 |
| -[subs="verbatim,quotes,macros"] |
24 |
| ----- |
25 |
| -Given _nums_ = *[0,0,1,1,1,2,2,3,3,4]*, |
| 23 | +assert k == expectedNums.length; |
| 24 | +for (int i = 0; i < k; i++) { |
| 25 | + assert nums[i] == expectedNums[i]; |
| 26 | +} |
| 27 | +.... |
26 | 28 |
|
27 |
| -Your function should return length = *`5`*, with the first five elements of _`nums`_ being modified to *`0`*, *`1`*, *`2`*, *`3`*, and *`4`* respectively. |
| 29 | +如果所有断言都通过,那么您的题解将被 *通过*。 |
28 | 30 |
|
29 |
| -It doesn't matter what values are set beyond the returned length. |
| 31 | +*示例 1:* |
30 | 32 |
|
31 |
| ----- |
| 33 | +.... |
| 34 | +输入:nums = [1,1,2] |
| 35 | +输出:2, nums = [1,2,_] |
| 36 | +解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 |
| 37 | +.... |
32 | 38 |
|
33 |
| -*Clarification:* |
| 39 | +*示例 2:* |
34 | 40 |
|
35 |
| -Confused why the returned value is an integer but your answer is an array? |
| 41 | +.... |
| 42 | +输入:nums = [0,0,1,1,1,2,2,3,3,4] |
| 43 | +输出:5, nums = [0,1,2,3,4] |
| 44 | +解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 |
| 45 | +.... |
36 | 46 |
|
37 |
| -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. |
38 | 47 |
|
39 |
| -Internally you can think of this: |
| 48 | +*提示:* |
40 | 49 |
|
41 |
| -[subs="verbatim,quotes,macros"] |
42 |
| ----- |
43 |
| -// *nums* is passed in by reference. (i.e., without making a copy) |
44 |
| -int len = removeDuplicates(nums); |
| 50 | +* `1 \<= nums.length \<= 3 * 10^4^` |
| 51 | +* `-10^4^ \<= nums[i] \<= 10^4^` |
| 52 | +* `nums` 已按 *非严格递增* 排列 |
45 | 53 |
|
46 |
| -// any modification to *nums* in your function would be known by the caller. |
47 |
| -// using the length returned by your function, it prints the first *len* elements. |
48 |
| -for (int i = 0; i < len; i++) { |
49 |
| - print(nums[i]); |
50 |
| -} |
51 |
| ----- |
52 | 54 |
|
53 |
| -== 解题分析 |
| 55 | +== 思路分析 |
| 56 | + |
| 57 | +双指针:把后面的数据向前拷贝。 |
54 | 58 |
|
55 |
| -把后面的数据向前拷贝。 |
| 59 | +image::images/0026-10.png[{image_attr}] |
56 | 60 |
|
57 | 61 | [[src-0026]]
|
| 62 | +[tabs] |
| 63 | +==== |
| 64 | +一刷:: |
| 65 | ++ |
| 66 | +-- |
58 | 67 | [{java_src_attr}]
|
59 | 68 | ----
|
60 | 69 | include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray.java[tag=answer]
|
61 | 70 | ----
|
| 71 | +-- |
62 | 72 |
|
| 73 | +二刷:: |
| 74 | ++ |
| 75 | +-- |
63 | 76 | [{java_src_attr}]
|
64 | 77 | ----
|
65 | 78 | include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray_2.java[tag=answer]
|
66 | 79 | ----
|
| 80 | +-- |
| 81 | +
|
| 82 | +三刷:: |
| 83 | ++ |
| 84 | +-- |
| 85 | +[{java_src_attr}] |
| 86 | +---- |
| 87 | +include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray_3.java[tag=answer] |
| 88 | +---- |
| 89 | +-- |
| 90 | +==== |
| 91 | + |
67 | 92 |
|
68 | 93 | == 参考资料
|
69 | 94 |
|
|
0 commit comments