Skip to content

Commit 3caffdd

Browse files
Add files via upload
1 parent 78af66a commit 3caffdd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 第一种思路
2+
# 76ms 60.32%
3+
class Solution:
4+
def removeDuplicates(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: int
8+
"""
9+
index = 1
10+
while index < len(nums):
11+
if nums[index] == nums[index - 1]:
12+
del nums[index]
13+
else:
14+
index += 1
15+
return len(nums)
16+
17+
# 第二种思路,双指针法
18+
# 60ms 98.07
19+
class Solution:
20+
def removeDuplicates(self, nums):
21+
"""
22+
:type nums: List[int]
23+
:rtype: int
24+
"""
25+
if not nums:
26+
return 0
27+
i = 0
28+
for j in range(1, len(nums)):
29+
if nums[j] != nums[i]:
30+
i += 1
31+
nums[i] = nums[j]
32+
return i + 1

0 commit comments

Comments
 (0)