Skip to content

Commit

Permalink
添加有序数组去重Python3代码
Browse files Browse the repository at this point in the history
  • Loading branch information
eric496 authored and labuladong committed Jun 22, 2020
1 parent 2ec69eb commit 4a50469
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 高频面试系列/如何去除有序数组的重复元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@ ListNode deleteDuplicates(ListNode head) {

![labuladong](../pictures/labuladong.png)

[eric wang](https://www.github.com/eric496) 提供有序数组 Python3 代码

```python
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)

if n == 0:
return 0

slow, fast = 0, 1

while fast < n:
if nums[fast] != nums[slow]:
slow += 1
nums[slow] = nums[fast]

fast += 1

return slow + 1
```

[eric wang](https://www.github.com/eric496) 提供有序链表 Python3 代码

```python
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return head

slow, fast = head, head.next

while fast:
if fast.val != slow.val:
slow.next = fast
slow = slow.next

fast = fast.next

# 断开与后面重复元素的连接
slow.next = None
return head
```

[上一篇:如何高效解决接雨水问题](../高频面试系列/接雨水.md)

[下一篇:如何寻找最长回文子串](../高频面试系列/最长回文子串.md)
Expand Down

0 comments on commit 4a50469

Please sign in to comment.