-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_element.py
More file actions
38 lines (33 loc) · 1.04 KB
/
remove_element.py
File metadata and controls
38 lines (33 loc) · 1.04 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
#!/usr/bin/env python
# encoding: utf8
# 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.
__author__ = "Zhu Xianfeng <xianfeng.zhu@gmail.com>"
class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
count = 0
last = -1
idx = -1
for val in A:
idx += 1
if val == elem:
if last == -1:
last = idx
count += 1
elif last != -1:
# val not equals elem && last index is not -1
A[last] = val
last += 1
return (idx + 1) - count
def main():
num = [1, 3, 2, 3, 9, 2, 10]
sol = Solution()
count = sol.removeElement(num, 2)
print num
print count
if __name__ == "__main__":
main()