-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26.删除排序数组中的重复项.py
44 lines (31 loc) · 1.09 KB
/
26.删除排序数组中的重复项.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
# @lc app=leetcode.cn id=26 lang=python3
#
# [26] 删除排序数组中的重复项
#
# @lc code=start
class Solution:
# def removeDuplicates(self, nums: List[int]) -> int:
# # 正向遍历数组,数组删除会导致数组长度变化,所以要反向删除
# #另外注意range(10,0,-1) 是左闭右开[10,0)
# for i in range(len(nums)-1,0,-1):
# print(nums[i],nums[i-1])
# if nums[i]==nums[i-1]:
# nums.pop(i)
# return len(nums)
def removeDuplicates(self, nums: List[int]) -> int:
#双指针遍历
# if len(nums)==0:
# return 0
i = 0
for j in range(1,len(nums)):
if nums[i]!=nums[j]:
i = i +1
nums[i] = nums[j]
#因为最后要返回数组长度,所以i+1
return i+1
# 加上and
# 如果要删除多余的元素,可以加上,但题目中说不考虑数组长度
# del nums[i+1:]
# return len(nums) and i + 1
# @lc code=end