Skip to content

Commit f178674

Browse files
committedMay 17, 2025·
三刷26
1 parent 98885cd commit f178674

File tree

6 files changed

+103
-39
lines changed

6 files changed

+103
-39
lines changed
 

‎docs/0026-remove-duplicates-from-sorted-array.adoc

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,94 @@
11
[#0026-remove-duplicates-from-sorted-array]
2-
= 26. Remove Duplicates from Sorted Array
2+
= 26. 删除有序数组中的重复项
33

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. 删除有序数组中的重复项 ^]
55

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` 中唯一元素的个数。
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` 的唯一元素的数量为 `k`,你需要做以下事情确保你的题解可以被通过:
99

10-
*Example 1:*
10+
* 更改数组 `nums` ,使 `nums` 的前 `k` 个元素包含唯一元素,并按照它们最初在 `nums` 中出现的顺序排列。`nums` 的其余元素与 `nums` 的大小不重要。
11+
* 返回 `k`
1112
12-
[subs="verbatim,quotes,macros"]
13-
----
14-
Given _nums_ = *[1,1,2]*,
13+
*判题标准:*
1514

16-
Your function should return length = *`2`*, with the first two elements of _`nums`_ being *`1`* and *`2`* respectively.
15+
系统会用下面的代码来测试你的题解:
1716

18-
It doesn't matter what you leave beyond the returned length.
19-
----
17+
....
18+
int[] nums = [...]; // 输入数组
19+
int[] expectedNums = [...]; // 长度正确的期望答案
2020
21-
*Example 2:*
21+
int k = removeDuplicates(nums); // 调用
2222
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+
....
2628

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+
如果所有断言都通过,那么您的题解将被 *通过*
2830

29-
It doesn't matter what values are set beyond the returned length.
31+
*示例 1:*
3032

31-
----
33+
....
34+
输入:nums = [1,1,2]
35+
输出:2, nums = [1,2,_]
36+
解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
37+
....
3238

33-
*Clarification:*
39+
*示例 2:*
3440

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

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

39-
Internally you can think of this:
48+
*提示:*
4049

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` 已按 *非严格递增* 排列
4553
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-
----
5254
53-
== 解题分析
55+
== 思路分析
56+
57+
双指针:把后面的数据向前拷贝。
5458

55-
把后面的数据向前拷贝。
59+
image::images/0026-10.png[{image_attr}]
5660

5761
[[src-0026]]
62+
[tabs]
63+
====
64+
一刷::
65+
+
66+
--
5867
[{java_src_attr}]
5968
----
6069
include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray.java[tag=answer]
6170
----
71+
--
6272
73+
二刷::
74+
+
75+
--
6376
[{java_src_attr}]
6477
----
6578
include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray_2.java[tag=answer]
6679
----
80+
--
81+
82+
三刷::
83+
+
84+
--
85+
[{java_src_attr}]
86+
----
87+
include::{sourcedir}/_0026_RemoveDuplicatesFromSortedArray_3.java[tag=answer]
88+
----
89+
--
90+
====
91+
6792

6893
== 参考资料
6994

‎docs/images/0026-10.png

39 KB
Loading

‎logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,16 @@ endif::[]
708708
|{doc_base_url}/0022-generate-parentheses.adoc[题解]
709709
|✅ 回溯。也可以去掉回溯过程,使用深度优先遍历。
710710

711-
|{counter:codes}
711+
|{counter:codes2503}
712712
|{leetcode_base_url}/swap-nodes-in-pairs/[24. 两两交换链表中的节点^]
713713
|{doc_base_url}/0024-swap-nodes-in-pairs.adoc[题解]
714714
|✅ 链表。增加虚拟节点,极大简化代码。
715715

716+
|{counter:codes2503}
717+
|{leetcode_base_url}/remove-duplicates-from-sorted-array/[26. 删除有序数组中的重复项^]
718+
|{doc_base_url}/0026-remove-duplicates-from-sorted-array.adoc[题解]
719+
|✅ 双指针
720+
716721
|===
717722

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

‎src/main/java/com/diguage/algo/leetcode/_0026_RemoveDuplicatesFromSortedArray.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@
6161
*/
6262
public class _0026_RemoveDuplicatesFromSortedArray {
6363
// tag::answer[]
64-
public static int removeDuplicates(int[] nums) {
64+
65+
/**
66+
* @author D瓜哥 · https://www.diguage.com
67+
* @since 2018-07-19 18:34
68+
*/
69+
public int removeDuplicates(int[] nums) {
6570
if (Objects.isNull(nums) || nums.length == 0) {
6671
return 0;
6772
}
@@ -75,7 +80,7 @@ public static int removeDuplicates(int[] nums) {
7580
return i + 1;
7681
}
7782

78-
public static int removeDuplicates1(int[] nums) {
83+
public int removeDuplicates1(int[] nums) {
7984
if (nums == null || nums.length == 0) {
8085
return 0;
8186
}
@@ -106,7 +111,7 @@ public static int removeDuplicates1(int[] nums) {
106111

107112
public static void main(String[] args) {
108113
int[] nums = {1, 2, 2};
109-
System.out.println(removeDuplicates(nums));
114+
new _0026_RemoveDuplicatesFromSortedArray().removeDuplicates(nums);
110115
System.out.println(Arrays.toString(nums));
111116
}
112117
}

‎src/main/java/com/diguage/algo/leetcode/_0026_RemoveDuplicatesFromSortedArray_2.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@
5858
*/
5959
public class _0026_RemoveDuplicatesFromSortedArray_2 {
6060
// tag::answer[]
61-
public static int removeDuplicates(int[] nums) {
61+
/**
62+
* @author D瓜哥 · https://www.diguage.com
63+
* @since 2024-07-03 14:23:14
64+
*/
65+
public int removeDuplicates(int[] nums) {
6266
if (nums == null || nums.length == 0) {
6367
return 0;
6468
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0026_RemoveDuplicatesFromSortedArray_3 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-17 20:48:52
8+
*/
9+
public int removeDuplicates(int[] nums) {
10+
int slow = 0, fast = 0;
11+
while (fast < nums.length) {
12+
if (nums[fast] != nums[slow]) {
13+
nums[++slow] = nums[fast];
14+
}
15+
fast++;
16+
}
17+
return slow + 1;
18+
}
19+
// end::answer[]
20+
public static void main(String[] args) {
21+
new _0026_RemoveDuplicatesFromSortedArray_3()
22+
.removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4});
23+
}
24+
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.