forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
26_Remove Duplicates from Sorted Array.py
55 lines (48 loc) · 1.3 KB
/
26_Remove Duplicates from Sorted Array.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
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 27 11:13:11 2019
@author: leiya
"""
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0
while i < len(nums)-1:
if nums[i] == nums[i+1]:
nums.pop(i)
else:
i += 1
return len(nums)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0
while i < len(nums)-1:
if nums[i] == nums[i+1]:
nums.pop(i+1)
else:
i += 1
return len(nums)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
count = 1
j = 0
for i in range(1,n):
if nums[i-j] != nums[i-j-1]:
count += 1
else:
nums.pop(i-j-1)
j += 1
return count
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
count = 0
#if we let count = 1,when len(nums)=1,nums[count] must be out of range
for i in range(len(nums)):
if nums[count] != nums[i]:
count += 1
nums[count] = nums[i]
return count+1