forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremove-element.py
More file actions
25 lines (23 loc) · 765 Bytes
/
remove-element.py
File metadata and controls
25 lines (23 loc) · 765 Bytes
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
from __future__ import print_function
# Time: O(n)
# Space: O(1)
#
# 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.
#
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):
i, last = 0, len(A) - 1
while i <= last:
if A[i] == elem:
A[i], A[last] = A[last], A[i]
last -= 1
else:
i += 1
return last + 1
if __name__ == "__main__":
print(Solution().removeElement([1, 2, 3, 4, 5, 2, 2], 2))