-
Notifications
You must be signed in to change notification settings - Fork 0
/
80.remove-duplicates-from-sorted-array-ii.py
61 lines (51 loc) · 1.61 KB
/
80.remove-duplicates-from-sorted-array-ii.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
56
57
58
59
60
#
# @lc app=leetcode id=80 lang=python3
#
# [80] Remove Duplicates from Sorted Array II
#
# @lc code=start
from typing import List
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
if n<2: return n
index, occurence = 1, 1
for i in range(1, n):
if nums[i] == nums[i-1]:
occurence+=1
else:
occurence = 1
if occurence <= 2:
nums[index] = nums[i]
index += 1
return index
def removeDuplicates_deprecate(self, nums: List[int]) -> int:
n = len(nums)
if n<=2: return nums
l, r= 0, 1 # nums 배열 왼쪽 이정표, 오른쪽 이정표
flag = 0 # 현재 중복 숫자 최대 2개 조건 만족하는지 여부 판별 이정표
val = nums[0] # 현재 기준 숫자
flag = 0
while r < n:
print(f"{l} {r}\n{nums}")
if nums[l] == nums[r]:
if flag == 0:
l+=1; r+=1
if val == nums[l]:
flag = 1
else:
while nums[l] == nums[r]:
r+=1
print(f" {l} {r}")
val = nums[r]
l += 1
nums[l] = nums[r]
flag = 0
else:
val = nums[r]
l += 1
nums[l] = nums[r]
flag = 0
# Solution().removeDuplicates([1,1,1,2,2,3])
# Solution().removeDuplicates([0,0,1,1,1,1,2,3,3])
# @lc code=end