-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_element.py
More file actions
41 lines (34 loc) · 1.01 KB
/
remove_element.py
File metadata and controls
41 lines (34 loc) · 1.01 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
# Given an array nums and a value val, remove all instances of
# that value in-place and return the new length.
#
# Do not allocate extra space for another array, you must do this by modifying
# the input array in-place with O(1) extra memory.
#
# The order of elements can be changed. It doesn't matter what you leave beyond
# the new length.
# Learning:
# I did not read the problem statement correctly and was returning different value
# than desired.
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
max = len(nums)
if max == 0:
return 0
i = 0
while i < max:
print str('*' * 60 + str(i))
if nums[i] == val:
del nums[i]
max = max - 1
else:
i = i + 1
return len(nums)
if __name__ == "__main__":
s = Solution()
nums = [0,1,2,2,3,0,4,2]
print(s.removeElement(nums, 2))