forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_dup_from_sorted.py
More file actions
185 lines (162 loc) · 4.88 KB
/
remove_dup_from_sorted.py
File metadata and controls
185 lines (162 loc) · 4.88 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
from __future__ import division
import random
from LinkedList import Node
'''
Leetcode: Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
'''
# O(1) in space!
def remove_dup_array(A):
print A, '-->',
i = 0
for j in range(1, len(A)):
if A[i] != A[j]:
i += 1
A[i] = A[j]
for j in range(i+1, len(A)):
A[j] = None
print A
return i+1, A
'''
Leetcode: Remove Duplicates from Sorted Array II
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
'''
# O(1) in space!
def remove_dup_array2(A):
if len(A) <= 2: return A
print A, '-->',
i = 1
for j in range(2, len(A)):
if (not A[j] == A[i-1]) or (not A[j] == A[i]):
i += 1
A[i] = A[j]
for j in range(i+1, len(A)):
A[j] = None
print A
return i+1, A
'''
Leetcode: Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
'''
def remove_dup(node):
seen = set()
root = node
last = node # last node of the non-duplicate linked list
while node:
if node.value == last.value:
last.next = node.next
else:
seen.add(node.value)
last = node
node = node.next
print root
return root
'''
Leetcode: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Given 1->1->2->2, return Null.
'''
# keep three pointers:
# (1) prev: previous node of cur
# (2) cur: first element of potential duplicated sequence
# (3) next: last element of potential duplicated sequence
def remove_dup2(head):
if not head: return None
dummy = Node(None, next=head)
prev = dummy
cur = head
next = head.next
seen = 0
while next:
if cur.value != next.value:
if seen > 0:
# delete duplicated numbers
prev.next = next
cur = next
next = next.next
else:
# simply move one step for every pointer
prev = prev.next
cur = cur.next
next = next.next
seen = 0
else:
# find the last element of the duplicated number
next = next.next
seen += 1
# leftover (duplications at the end)
if next is None and seen > 0:
prev.next = None
return dummy.next
'''
Leetcode: Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length.
'''
def remove_element(A, val):
print A, '-->',
i = 0
for j in range(1, len(A)):
if A[j] != val:
i += 1
A[i] = A[j]
for j in range(i+1, len(A)):
A[j] = None
print A
return i+1, A
'''
Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note: Given n will always be valid.
Try to do this in ONE pass.
'''
# take care of the first/last
def remove_nth_end(root, n):
first = root
second = root
i = 0
while second:
second = second.next
i += 1
if i == n+1: break
if i < n+1:
# the first element will be removed
root = first.next
else:
while second:
first = first.next
second = second.next
# remove first.next node
first.next = first.next.next
print root
return root
if __name__ == '__main__':
l1 = Node(1, Node(1, Node(1, Node(2))))
l2 = Node(2, Node(3, Node(3, Node(5, Node(5, Node(7))))))
l3 = Node(1, Node(1, Node(3, Node(3, Node(5, Node(5))))))
#remove_dup(l1)
#remove_dup(l2)
#remove_dup(l3)
remove_dup2(l1)
remove_dup2(l2)
remove_dup2(l3)
#remove_nth_end(l1, 3)
#remove_nth_end(l2, 3)
#remove_nth_end(l3, 1)
#remove_dup_array2([1,1,2,2,2])
#remove_dup_array2([2,2,2,3,4,5,5,5,6])
#remove_element([1,1,3,2,2,4,3,5,1,7], 3)
#remove_element([5,4,3,3], 9)