|
1 | 1 | [#0080-remove-duplicates-from-sorted-array-ii]
|
2 |
| -= 80. Remove Duplicates from Sorted Array II |
| 2 | += 80. 删除有序数组中的重复项 II |
3 | 3 |
|
4 |
| -{leetcode}/problems/remove-duplicates-from-sorted-array-ii/[LeetCode - Remove Duplicates from Sorted Array II^] |
| 4 | +https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/[LeetCode - 80. 删除有序数组中的重复项 II ^] |
5 | 5 |
|
6 |
| -Given a sorted array `nums`, remove the duplicates in-place such that duplicates appeared at most *twice* and return the new length. |
| 6 | +给你一个有序数组 `nums` ,请你 *http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地]* 删除重复出现的元素,使得出现次数超过两次的元素**只出现两次**,返回删除后数组的新长度。 |
7 | 7 |
|
8 |
| -Do not allocate extra space for another array, you must do this by *modifying the input array* in-place with O(1) extra memory. |
| 8 | +不要使用额外的数组空间,你必须在 *https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地] 修改输入数组* 并在使用 O(1) 额外空间的条件下完成。 |
9 | 9 |
|
10 |
| -.Example 1: |
11 |
| ----- |
12 |
| -Given nums = [1,1,1,2,2,3], |
13 |
| -
|
14 |
| -Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. |
15 |
| -
|
16 |
| -It doesn't matter what you leave beyond the returned length. |
17 |
| ----- |
| 10 | +*说明:* |
18 | 11 |
|
19 |
| -.Example 2: |
20 |
| ----- |
21 |
| -Given nums = [0,0,1,1,1,1,2,3,3], |
| 12 | +为什么返回数值是整数,但输出的答案是数组呢? |
22 | 13 |
|
23 |
| -Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. |
| 14 | +请注意,输入数组是以**「引用」**方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。 |
24 | 15 |
|
25 |
| -It doesn't matter what values are set beyond the returned length. |
26 |
| ----- |
| 16 | +你可以想象内部操作如下: |
27 | 17 |
|
28 |
| -*Clarification:* |
29 |
| - |
30 |
| -Confused why the returned value is an integer but your answer is an array? |
31 |
| - |
32 |
| -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. |
33 |
| - |
34 |
| -Internally you can think of this: |
35 |
| - |
36 |
| ----- |
37 |
| -// nums is passed in by reference. (i.e., without making a copy) |
| 18 | +.... |
| 19 | +// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝 |
38 | 20 | int len = removeDuplicates(nums);
|
39 | 21 |
|
40 |
| -// any modification to nums in your function would be known by the caller. |
41 |
| -// using the length returned by your function, it prints the first len elements. |
| 22 | +// 在函数里修改输入数组对于调用者是可见的。 |
| 23 | +// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。 |
42 | 24 | for (int i = 0; i < len; i++) {
|
43 | 25 | print(nums[i]);
|
44 | 26 | }
|
45 |
| ----- |
| 27 | +.... |
46 | 28 |
|
47 |
| -== 解题分析 |
48 | 29 |
|
49 |
| -解题思路很简单:把后面的值覆盖前面多余的值。关键是如何用简单的代码来实现这个思路。 |
| 30 | +*示例 1:* |
50 | 31 |
|
51 |
| -== 参考资料 |
| 32 | +.... |
| 33 | +输入:nums = [1,1,1,2,2,3] |
| 34 | +输出:5, nums = [1,1,2,2,3] |
| 35 | +解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3。 不需要考虑数组中超出新长度后面的元素。 |
| 36 | +.... |
52 | 37 |
|
53 |
| -. https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-i-7/[删除排序数组中的重复项 II - 删除排序数组中的重复项 II - 力扣(LeetCode)^] |
| 38 | +*示例 2:* |
54 | 39 |
|
55 |
| -Given a sorted array _nums_, remove the duplicates https://en.wikipedia.org/wiki/In-place_algorithm[*in-place*^] such that duplicates appeared at most _twice_ and return the new length. |
| 40 | +.... |
| 41 | +输入:nums = [0,0,1,1,1,1,2,3,3] |
| 42 | +输出:7, nums = [0,0,1,1,2,3,3] |
| 43 | +解释:函数应返回新长度 length = 7, 并且原数组的前七个元素被修改为 0, 0, 1, 1, 2, 3, 3。不需要考虑数组中超出新长度后面的元素。 |
| 44 | +.... |
56 | 45 |
|
57 |
| -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. |
58 | 46 |
|
59 |
| -*Example 1:* |
| 47 | +*提示:* |
60 | 48 |
|
61 |
| -[subs="verbatim,quotes,macros"] |
62 |
| ----- |
63 |
| -Given _nums_ = *[1,1,1,2,2,3]*, |
| 49 | +* `1 \<= nums.length \<= 3 * 10^4^` |
| 50 | +* `-10^4^ \<= nums[i] \<= 10^4^` |
| 51 | +* `nums` 已按升序排列 |
64 | 52 |
|
65 |
| -Your function should return length = *`5`*, with the first five elements of _`nums`_ being *`1, 1, 2, 2`* and *3* respectively. |
66 | 53 |
|
67 |
| -It doesn't matter what you leave beyond the returned length. |
68 |
| ----- |
| 54 | +== 思路分析 |
69 | 55 |
|
70 |
| -*Example 2:* |
| 56 | +解题思路很简单:把后面的值覆盖前面多余的值。关键是如何用简单的代码来实现这个思路。 |
71 | 57 |
|
72 |
| -[subs="verbatim,quotes,macros"] |
| 58 | +[[src-0080]] |
| 59 | +[tabs] |
| 60 | +==== |
| 61 | +一刷:: |
| 62 | ++ |
| 63 | +-- |
| 64 | +[{java_src_attr}] |
73 | 65 | ----
|
74 |
| -Given _nums_ = *[0,0,1,1,1,1,2,3,3]*, |
75 |
| -
|
76 |
| -Your function should return length = *`7`*, with the first seven elements of _`nums`_ being modified to *`0`*, *0*, *1*, *1*, *2*, *3* and *3* respectively. |
77 |
| -
|
78 |
| -It doesn't matter what values are set beyond the returned length. |
79 |
| -
|
| 66 | +include::{sourcedir}/_0080_RemoveDuplicatesFromSortedArrayII.java[tag=answer] |
80 | 67 | ----
|
| 68 | +-- |
81 | 69 |
|
82 |
| -*Clarification:* |
83 |
| - |
84 |
| -Confused why the returned value is an integer but your answer is an array? |
85 |
| - |
86 |
| -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. |
87 |
| - |
88 |
| -Internally you can think of this: |
89 |
| - |
90 |
| -[subs="verbatim,quotes,macros"] |
| 70 | +二刷:: |
| 71 | ++ |
| 72 | +-- |
| 73 | +[{java_src_attr}] |
91 | 74 | ----
|
92 |
| -// *nums* is passed in by reference. (i.e., without making a copy) |
93 |
| -int len = removeDuplicates(nums); |
94 |
| -
|
95 |
| -// any modification to *nums* in your function would be known by the caller. |
96 |
| -// using the length returned by your function, it prints the first *len* elements. |
97 |
| -for (int i = 0; i < len; i++) { |
98 |
| - print(nums[i]); |
99 |
| -} |
100 |
| -
|
| 75 | +include::{sourcedir}/_0080_RemoveDuplicatesFromSortedArrayIi_2.java[tag=answer] |
101 | 76 | ----
|
| 77 | +-- |
| 78 | +==== |
102 | 79 |
|
103 | 80 |
|
104 |
| -[[src-0080]] |
105 |
| -[{java_src_attr}] |
106 |
| ----- |
107 |
| -include::{sourcedir}/_0080_RemoveDuplicatesFromSortedArrayII.java[tag=answer] |
108 |
| ----- |
| 81 | +== 参考资料 |
109 | 82 |
|
| 83 | +. https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-i-7/[删除排序数组中的重复项 II - 删除排序数组中的重复项 II - 力扣(LeetCode)^] |
0 commit comments