Skip to content

Commit c34b3f2

Browse files
authored
Update 12. 删除有序数组中的重复项.md
1 parent 3dc31de commit c34b3f2

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

12. 删除有序数组中的重复项.md

+7-15
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@ class Solution:
1111
def removeDuplicates(self, nums: List[int]) -> int:
1212
#双指针法
1313
n = len(nums)
14-
if n==1:
15-
return 1
16-
left = 0
17-
right = 1
18-
#循环终止条件
19-
while right < n:
20-
#如果不相等,将right位置的元素复制到left+1位置上,left后移一位
21-
if nums[right] != nums[left]:
22-
nums[left+1] = nums[right]
23-
left += 1
24-
#right后移一位
25-
right += 1
26-
#返回新数组长度
27-
return left+1
28-
```
14+
i = 0
15+
for j in range(n):
16+
if nums[j] != nums[i]:
17+
nums[i+1] = nums[j]
18+
i +=1
19+
return i+1
20+
```

0 commit comments

Comments
 (0)