Skip to content

Commit bdae3f9

Browse files
committed
二刷80
1 parent aaa00d4 commit bdae3f9

File tree

4 files changed

+89
-75
lines changed

4 files changed

+89
-75
lines changed
Lines changed: 49 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,83 @@
11
[#0080-remove-duplicates-from-sorted-array-ii]
2-
= 80. Remove Duplicates from Sorted Array II
2+
= 80. 删除有序数组中的重复项 II
33

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 ^]
55

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[原地]* 删除重复出现的元素,使得出现次数超过两次的元素**只出现两次**,返回删除后数组的新长度。
77

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) 额外空间的条件下完成。
99

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+
*说明:*
1811

19-
.Example 2:
20-
----
21-
Given nums = [0,0,1,1,1,1,2,3,3],
12+
为什么返回数值是整数,但输出的答案是数组呢?
2213

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+
请注意,输入数组是以**「引用」**方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
2415

25-
It doesn't matter what values are set beyond the returned length.
26-
----
16+
你可以想象内部操作如下:
2717

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 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
3820
int len = removeDuplicates(nums);
3921
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+
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
4224
for (int i = 0; i < len; i++) {
4325
print(nums[i]);
4426
}
45-
----
27+
....
4628

47-
== 解题分析
4829

49-
解题思路很简单:把后面的值覆盖前面多余的值。关键是如何用简单的代码来实现这个思路。
30+
*示例 1:*
5031

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+
....
5237

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:*
5439

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+
....
5645

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.
5846

59-
*Example 1:*
47+
*提示:*
6048

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` 已按升序排列
6452
65-
Your function should return length = *`5`*, with the first five elements of _`nums`_ being *`1, 1, 2, 2`* and *3* respectively.
6653
67-
It doesn't matter what you leave beyond the returned length.
68-
----
54+
== 思路分析
6955

70-
*Example 2:*
56+
解题思路很简单:把后面的值覆盖前面多余的值。关键是如何用简单的代码来实现这个思路。
7157

72-
[subs="verbatim,quotes,macros"]
58+
[[src-0080]]
59+
[tabs]
60+
====
61+
一刷::
62+
+
63+
--
64+
[{java_src_attr}]
7365
----
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]
8067
----
68+
--
8169
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}]
9174
----
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]
10176
----
77+
--
78+
====
10279

10380

104-
[[src-0080]]
105-
[{java_src_attr}]
106-
----
107-
include::{sourcedir}/_0080_RemoveDuplicatesFromSortedArrayII.java[tag=answer]
108-
----
81+
== 参考资料
10982

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)^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,11 @@ endif::[]
723723
|{doc_base_url}/0045-jump-game-ii.adoc[题解]
724724
|✅ 贪心算法。也可以动态规划。
725725

726+
|{counter:codes2503}
727+
|{leetcode_base_url}/remove-duplicates-from-sorted-array-ii/[80. 删除有序数组中的重复项 II^]
728+
|{doc_base_url}/0080-remove-duplicates-from-sorted-array-ii.adoc[题解]
729+
|✅ 双指针,同时需要记录每个元素的次数,相同元素只复制两个。
730+
726731
|===
727732

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class _0080_RemoveDuplicatesFromSortedArrayII {
1212
* Memory Usage: 41.7 MB, less than 5.26% of Java online submissions for Remove Duplicates from Sorted Array II.
1313
*
1414
* Copy from: 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)]
15+
*
16+
* @author D瓜哥 · https://www.diguage.com
17+
* @since 2020-02-04 20:57
1518
*/
1619
public int removeDuplicates(int[] nums) {
1720
int j = 1, count = 1;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0080_RemoveDuplicatesFromSortedArrayIi_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-18 15:08:39
9+
*/
10+
public int removeDuplicates(int[] nums) {
11+
int slow = 0, fast = 0, cnt = 1;
12+
while (fast < nums.length) {
13+
if (nums[fast] != nums[slow]) {
14+
cnt = 1;
15+
nums[++slow] = nums[fast];
16+
} else {
17+
if (cnt < 2 && slow != fast) {
18+
nums[++slow] = nums[fast];
19+
cnt++;
20+
}
21+
}
22+
fast++;
23+
}
24+
return slow + 1;
25+
}
26+
27+
// end::answer[]
28+
public static void main(String[] args) {
29+
new _0080_RemoveDuplicatesFromSortedArrayIi_2()
30+
.removeDuplicates(new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3});
31+
}
32+
}

0 commit comments

Comments
 (0)