Skip to content

Commit 013f7eb

Browse files
authored
Merge pull request #745 from LetMeFly666/80
添加问题“80.删除有序数组中的重复项II”的代码和题解
2 parents b51c7e7 + 641e05e commit 013f7eb

16 files changed

Lines changed: 353 additions & 61 deletions

Codes/0080-remove-duplicates-from-sorted-array-ii.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* @Author: LetMeFly
33
* @Date: 2022-06-19 09:51:38
44
* @LastEditors: LetMeFly
5-
* @LastEditTime: 2022-06-19 10:31:22
5+
* @LastEditTime: 2025-02-09 08:53:04
66
*/
77
#ifdef _WIN32
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// TODO: 写题解
11+
// 写题解 - 2022年的题解计划于2025年完成了。
1212

1313
#ifdef SecondTry // 典型错误,数组的修改导致误认为前面已有两个相同的了
1414
class Solution {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-02-09 08:44:41
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-02-09 08:48:30
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int removeDuplicates(vector<int>& nums) {
14+
int slow = 1;
15+
for (int fast = 2; fast < nums.size(); fast++) {
16+
if (nums[fast] != nums[slow - 1]) {
17+
nums[++slow] = nums[fast];
18+
}
19+
}
20+
return min((int)nums.size(), slow + 1);
21+
}
22+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-02-09 08:53:44
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-02-09 09:00:09
6+
*/
7+
package main
8+
9+
func removeDuplicates(nums []int) int {
10+
slow := 1
11+
for fast := 2; fast < len(nums); fast++ {
12+
if nums[fast] != nums[slow - 1] {
13+
slow++
14+
nums[slow] = nums[fast]
15+
}
16+
}
17+
return min(slow + 1, len(nums))
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-02-09 08:53:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-02-09 08:58:07
6+
*/
7+
class Solution {
8+
public int removeDuplicates(int[] nums) {
9+
int slow = 1;
10+
for (int fast = 2; fast < nums.length; fast++) {
11+
if (nums[fast] != nums[slow - 1]) {
12+
nums[++slow] = nums[fast];
13+
}
14+
}
15+
return Math.min(slow + 1, nums.length);
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-02-09 08:53:38
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-02-09 08:55:23
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def removeDuplicates(self, nums: List[int]) -> int:
11+
slow = 1
12+
for fast in range(2, len(nums)):
13+
if nums[fast] != nums[slow - 1]:
14+
slow += 1
15+
nums[slow] = nums[fast]
16+
return min(len(nums), slow + 1)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
|0063.不同路径II|中等|<a href="https://leetcode.cn/problems/unique-paths-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/08/LeetCode%200063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145509662" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/unique-paths-ii/solutions/3067362/letmefly-63bu-tong-lu-jing-iidong-tai-gu-je2m/" target="_blank">LeetCode题解</a>|
6262
|0067.二进制求和|简单|<a href="https://leetcode.cn/problems/add-binary/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/14/LeetCode%200067.%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%B1%82%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125793685" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/add-binary/solution/by-tisfy-o5z5/" target="_blank">LeetCode题解</a>|
6363
|0070.爬楼梯|简单|<a href="https://leetcode.cn/problems/climbing-stairs/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/10/LeetCode%200070.%E7%88%AC%E6%A5%BC%E6%A2%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134913892" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/climbing-stairs/solutions/2561558/letmefly-70pa-lou-ti-dong-tai-gui-hua-di-7d8m/" target="_blank">LeetCode题解</a>|
64+
|0080.删除有序数组中的重复项II|中等|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/09/LeetCode%200080.%E5%88%A0%E9%99%A4%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E9%A1%B9II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145528267" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/solutions/3068455/letmefly-80shan-chu-you-xu-shu-zu-zhong-b1e2i/" target="_blank">LeetCode题解</a>|
6465
|0082.删除排序链表中的重复元素II|中等|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/15/LeetCode%200082.%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135612345" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/solutions/2605736/letmefly-82shan-chu-pai-xu-lian-biao-zho-92xm/" target="_blank">LeetCode题解</a>|
6566
|0083.删除排序链表中的重复元素|简单|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/14/LeetCode%200083.%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135581000" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solutions/2603584/letmefly-83shan-chu-pai-xu-lian-biao-zho-cey8/" target="_blank">LeetCode题解</a>|
6667
|0086.分隔链表|中等|<a href="https://leetcode.cn/problems/partition-list/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/26/LeetCode%200086.%E5%88%86%E9%9A%94%E9%93%BE%E8%A1%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125467594" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/partition-list/solution/letmefly-86fen-ge-lian-biao-by-tisfy-64z3/" target="_blank">LeetCode题解</a>|
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
---
2+
title: 80.删除有序数组中的重复项 II:双指针 - C++/Java5 行版
3+
date: 2025-02-09 09:08:46
4+
tags: [题解, LeetCode, 中等, 数组, 双指针]
5+
---
6+
7+
# 【LetMeFly】80.删除有序数组中的重复项 II:双指针 - C++/Java5 行版
8+
9+
力扣题目链接:[https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/](https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/)
10+
11+
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地</a></strong> 删除重复出现的元素,使得出现次数超过两次的元素<strong>只出现两次</strong> ,返回删除后数组的新长度。</p>
12+
13+
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>说明:</strong></p>
18+
19+
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
20+
21+
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
22+
23+
<p>你可以想象内部操作如下:</p>
24+
25+
<pre>
26+
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
27+
int len = removeDuplicates(nums);
28+
29+
// 在函数里修改输入数组对于调用者是可见的。
30+
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
31+
for (int i = 0; i &lt; len; i++) {
32+
&nbsp; &nbsp; print(nums[i]);
33+
}
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>示例 1:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>nums = [1,1,1,2,2,3]
42+
<strong>输出:</strong>5, nums = [1,1,2,2,3]
43+
<strong>解释:</strong>函数应返回新长度 length = <strong><code>5</code></strong>, 并且原数组的前五个元素被修改为 <strong><code>1, 1, 2, 2, 3</code></strong>。 不需要考虑数组中超出新长度后面的元素。
44+
</pre>
45+
46+
<p><strong>示例 2:</strong></p>
47+
48+
<pre>
49+
<strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]
50+
<strong>输出:</strong>7, nums = [0,0,1,1,2,3,3]
51+
<strong>解释:</strong>函数应返回新长度 length = <strong><code>7</code></strong>, 并且原数组的前七个元素被修改为&nbsp;<strong><code>0, 0, 1, 1, 2, 3, 3</code></strong>。不需要考虑数组中超出新长度后面的元素。
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ul>
59+
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
60+
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
61+
<li><code>nums</code> 已按升序排列</li>
62+
</ul>
63+
64+
65+
66+
## 解题方法一:快慢指针
67+
68+
使用一个慢指针指向数组中已经合法的元素(初始值为1,因为前两个元素必合法),使用一个快指针指向当前遍历到的元素。
69+
70+
快指针从下标`2`开始遍历,如果快指针指向元素和慢指针指向元素的前一个元素不同,则快指针指向元素可以合法地加入数组(慢指针右移一位,快指针元素放到慢指针位置)。
71+
72+
+ 时间复杂度$O(len(nums))$
73+
+ 空间复杂度$O(1)$
74+
75+
### AC代码
76+
77+
#### C++
78+
79+
```cpp
80+
/*
81+
* @Author: LetMeFly
82+
* @Date: 2025-02-09 08:44:41
83+
* @LastEditors: LetMeFly.xyz
84+
* @LastEditTime: 2025-02-09 08:48:30
85+
*/
86+
class Solution {
87+
public:
88+
int removeDuplicates(vector<int>& nums) {
89+
int slow = 1;
90+
for (int fast = 2; fast < nums.size(); fast++) {
91+
if (nums[fast] != nums[slow - 1]) {
92+
nums[++slow] = nums[fast];
93+
}
94+
}
95+
return min((int)nums.size(), slow + 1);
96+
}
97+
};
98+
```
99+
100+
#### Python
101+
102+
```python
103+
'''
104+
Author: LetMeFly
105+
Date: 2025-02-09 08:53:38
106+
LastEditors: LetMeFly.xyz
107+
LastEditTime: 2025-02-09 08:55:23
108+
'''
109+
from typing import List
110+
111+
class Solution:
112+
def removeDuplicates(self, nums: List[int]) -> int:
113+
slow = 1
114+
for fast in range(2, len(nums)):
115+
if nums[fast] != nums[slow - 1]:
116+
slow += 1
117+
nums[slow] = nums[fast]
118+
return min(len(nums), slow + 1)
119+
```
120+
121+
#### Java
122+
123+
```java
124+
/*
125+
* @Author: LetMeFly
126+
* @Date: 2025-02-09 08:53:42
127+
* @LastEditors: LetMeFly.xyz
128+
* @LastEditTime: 2025-02-09 08:58:07
129+
*/
130+
class Solution {
131+
public int removeDuplicates(int[] nums) {
132+
int slow = 1;
133+
for (int fast = 2; fast < nums.length; fast++) {
134+
if (nums[fast] != nums[slow - 1]) {
135+
nums[++slow] = nums[fast];
136+
}
137+
}
138+
return Math.min(slow + 1, nums.length);
139+
}
140+
}
141+
```
142+
143+
#### Go
144+
145+
```go
146+
/*
147+
* @Author: LetMeFly
148+
* @Date: 2025-02-09 08:53:44
149+
* @LastEditors: LetMeFly.xyz
150+
* @LastEditTime: 2025-02-09 09:00:09
151+
*/
152+
package main
153+
154+
func removeDuplicates(nums []int) int {
155+
slow := 1
156+
for fast := 2; fast < len(nums); fast++ {
157+
if nums[fast] != nums[slow - 1] {
158+
slow++
159+
nums[slow] = nums[fast]
160+
}
161+
}
162+
return min(slow + 1, len(nums))
163+
}
164+
```
165+
166+
## 解题方法二:易想版本
167+
168+
> 来[自](https://github.com/LetMeFly666/LeetCode/blob/64231493342ec7c9a18147c5f32d4e6211df7e33/Codes/0080-remove-duplicates-from-sorted-array-ii.cpp)2022.6.19,当时就想[写题解](https://github.com/LetMeFly666/LeetCode/blob/64231493342ec7c9a18147c5f32d4e6211df7e33/Codes/0080-remove-duplicates-from-sorted-array-ii.cpp#L11)了。
169+
170+
如果[方法一](#解题方法一快慢指针)难想怎么办?很简单,使用两个指针指向“相同连续元素”的始末位置总可以了吧。
171+
172+
每次得到一种元素的始末位置后,就放置`min(2, 这种元素个数)`个这种元素到“合法数组”中就好了。
173+
174+
+ 时间复杂度$O(len(nums))$
175+
+ 空间复杂度$O(1)$
176+
177+
### [AC](https://github.com/LetMeFly666/LeetCode/blob/64231493342ec7c9a18147c5f32d4e6211df7e33/Codes/0080-remove-duplicates-from-sorted-array-ii.cpp#L1-L6)[代码](https://github.com/LetMeFly666/LeetCode/blob/64231493342ec7c9a18147c5f32d4e6211df7e33/Codes/0080-remove-duplicates-from-sorted-array-ii.cpp#L42-L58)
178+
179+
#### C++
180+
181+
```cpp
182+
/*
183+
* @Author: LetMeFly
184+
* @Date: 2022-06-19 09:51:38
185+
* @LastEditors: LetMeFly
186+
* @LastEditTime: 2022-06-19 10:31:22
187+
*/
188+
189+
class Solution {
190+
public:
191+
int removeDuplicates(vector<int>& nums) {
192+
int cnt = 0;
193+
int l = 0, r = 1; // [l, r)相同
194+
nums.push_back(1e5);
195+
for (; r < nums.size(); r++) {
196+
if (nums[r] != nums[l]) {
197+
for (int i = 0; i < min(2, r - l); i++) {
198+
nums[cnt++] = nums[r - 1];
199+
}
200+
l = r;
201+
}
202+
}
203+
return cnt;
204+
}
205+
};
206+
```
207+
208+
## End
209+
210+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/145528267)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/02/09/LeetCode%200080.%E5%88%A0%E9%99%A4%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E9%A1%B9II/)哦~
211+
>
212+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)
213+
>
214+
> Tisfy:[https://blog.letmefly.xyz/2025/02/09/LeetCode 0080.删除有序数组中的重复项II](https://blog.letmefly.xyz/2025/02/09/LeetCode%200080.%E5%88%A0%E9%99%A4%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E9%A1%B9II/)

Solutions/Other-Accumulation-Messy.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,52 @@ Go中`a := b`,若`b`是值类型则会发生值拷贝,修改`a`不会修改`
17261726
+ 常见值类型:基本类型(int、string、...)、数组、结构体
17271727
+ 常见引用类型:切片、映射、通道、函数、指针
17281728

1729+
### Kitex使用日记
1730+
1731+
From: [Github@LetMeFly666/LeetCode/tryGoPy/Go/douyinec/README.md](https://github.com/LetMeFly666/LeetCode/blob/b51c7e73c9798b0fd397ab2e131ba33868e47ad3/tryGoPy/Go/douyinec/README.md)
1732+
1733+
```bash
1734+
git clone git@cloudwego/kitex-examples.git
1735+
cd kitex-example
1736+
go run . # 这里我本来使用的Go 1.19.5不支持go.mod里的toolchain,所以升级为了当前最新版go1.23.5
1737+
# 令起终端
1738+
go run ./client
1739+
```
1740+
1741+
#### 开了个私有仓库来记录kitex学习过程中的修改
1742+
1743+
[Github@LetMeFly666/kitex-examples](https://github.com/LetMeFly666/kitex-examples/)
1744+
1745+
起了个别名:
1746+
1747+
```bash
1748+
git config --local alias.pushLet "push Let main:master"
1749+
```
1750+
1751+
后续想要push到远端的时候直接`git pushLet`就好。
1752+
1753+
TODOED: 给CloudWeGo提PR,不sleep 1 秒。
1754+
1755+
可行吗?试试吧。像个办法看进程启动了还是异常退出了。
1756+
1757+
不行就不PR了。
1758+
1759+
TODOED: https://www.cloudwego.io/zh/docs/kitex/getting-started/tutorial/#%E6%8B%89%E5%8F%96%E4%BE%9D%E8%B5%96 的上面的代码段,build.sh后面有一个tab,导致渲染出来的注释不对齐
1760+
1761+
#### 自定义RPC函数
1762+
1763+
1. 修改`.thrift`文件
1764+
2. 使用`kitex`生成新代码文件
1765+
1766+
```bash
1767+
kitex -module "github.com/cloudwego/kitex-examples" -service a.b.c hello.thrift
1768+
```
1769+
1770+
3. 更新`go`文件
1771+
1772+
1. 更新`./handler.go`,实现自定义的RPC函数
1773+
2. 更新`./client/main.go`,调用这个远程函数
1774+
17291775
## sth. about 漯河日报(特指网站) - 发现By [我](https://letmefly.xyz/)和[shy](https://web.letmefly.xyz/He0/shykeke/)
17301776

17311777
由一本书引发了漯河日报的[一篇文章](http://rb.lhrb.com.cn/html/2018-06/28/content_25454.htm),然后开始了一点点对于日报官网的探究。

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ tags: [其他, 知识, 英语, Notes]
990990
|sculptor|n. 雕刻家,雕塑家|
991991
|ashore|adv. 在岸上,上岸|
992992
|tramp|n. 流浪汉,沉重的脚步声,长途跋涉<br/>v. 脚步沉重地行走,跋涉,踏|
993+
|||
994+
|fathom| n. 英寻(测量水深单位,合6英尺或1.8米)<br/>v. 理解,彻底了解,弄清真相|
993995

994996
<p class="wordCounts">单词收录总数</p>
995997

0 commit comments

Comments
 (0)