Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ee27089

Browse files
🐱(array): 80. 删除排序数组中的重复项 II
补充解法
1 parent 320e6d3 commit ee27089

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

‎docs/data-structure/array/README.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ class Solution(object):
10151015
- 此时要删除 `nums[i + 2]` 就要向后找到一个 `nums[j]`(满足 `nums[j] != nums[i]`)替换 `nums[i + 2]`
10161016

10171017
```python
1018+
# 这个好理解一些
10181019
class Solution(object):
10191020
def removeDuplicates(self, nums):
10201021
"""
@@ -1032,7 +1033,27 @@ class Solution(object):
10321033
nums[i + 2] = nums[j]
10331034
i = i + 1
10341035

1035-
return i+2
1036+
return i + 2
1037+
```
1038+
1039+
补充解法:
1040+
1041+
```python
1042+
class Solution:
1043+
def removeDuplicates(self, nums: List[int]) -> int:
1044+
i, count = 1, 1
1045+
for j in range(1, len(nums)):
1046+
if nums[j] == nums[j - 1]:
1047+
count += 1
1048+
else:
1049+
count = 1
1050+
1051+
if count <= 2:
1052+
# 相同的数 <= 2,跳过 i
1053+
nums[i] = nums[j]
1054+
i += 1
1055+
1056+
return i
10361057
```
10371058

10381059

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /