Skip to content

Commit c7564c2

Browse files
committed
update status/code Array/remove-element.py
1 parent 28b8cca commit c7564c2

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
016 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium |`Two Pointers`, `basic`, `trick`| AGAIN**
8888
018| [4 Sum](https://leetcode.com/problems/4sum/) | [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/4sum.py) | _O(n^3)_ | _O(1)_ | Medium | `k sum`, `Two Pointers`, `trick`, `hard` | AGAIN (not start)
8989
026 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-duplicates-from-sorted-array.py) | _O(n)_ | _O(1)_ | Easy | `Two Pointers`, `basic`, `trick` | AGAIN*
90-
027 | [Remove Element](https://leetcode.com/problems/remove-element/) | [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy ||
90+
027 | [Remove Element](https://leetcode.com/problems/remove-element/) |[Python](https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/remove-element.py) | _O(n)_ | _O(1)_ | Easy |`basic`| AGAIN*
9191
031 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [Python](./Python/next-permutation.py) | _O(n)_ | _O(1)_ | Medium | Tricky |
9292
048 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [Python](./Python/rotate-image.py) | _O(n^2)_ | _O(1)_ | Medium ||
9393
054 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](./Python/spiral-matrix.py) | _O(m * n)_ | _O(1)_ | Medium ||

leetcode_python/Array/remove-element.py

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,70 @@
66
# The order of elements can be changed. It doesn't matter what you leave beyond the new length.
77
#
88

9+
# V0
910

10-
# V1 : dev (?)
11-
12-
13-
class Solution:
11+
# V1
12+
# https://blog.csdn.net/coder_orz/article/details/51578854
13+
# IDEA : DOUBLE POINTER
14+
class Solution(object):
1415
def removeElement(self, nums, val):
15-
return [ k for k in nums if k != val]
16-
17-
18-
# V2
19-
16+
"""
17+
:type nums: List[int]
18+
:type val: int
19+
:rtype: int
20+
"""
21+
length = 0
22+
for i in range(len(nums)):
23+
if nums[i] != val:
24+
nums[length] = nums[i]
25+
length += 1
26+
return length
27+
28+
# V1'
29+
# https://blog.csdn.net/coder_orz/article/details/51578854
2030
class Solution(object):
2131
def removeElement(self, nums, val):
22-
for x in nums[:]:
23-
if x == val:
24-
nums.remove(x)
25-
return len(nums)
26-
27-
32+
"""
33+
:type nums: List[int]
34+
:type val: int
35+
:rtype: int
36+
"""
37+
rm_index = []
38+
for i in range(len(nums)):
39+
if nums[i] == val:
40+
rm_index.append(i)
41+
last = len(nums) - 1
42+
for i in rm_index:
43+
while last >= 0 and nums[last] == val:
44+
last -= 1
45+
if last < 0:
46+
break
47+
nums[i] = nums[last]
48+
last -= 1
49+
return len(nums) - len(rm_index)
2850

51+
# V1''
52+
# https://blog.csdn.net/coder_orz/article/details/51578854
53+
class Solution(object):
54+
def removeElement(self, nums, val):
55+
"""
56+
:type nums: List[int]
57+
:type val: int
58+
:rtype: int
59+
"""
60+
length, i = len(nums), 0
61+
while i < length:
62+
if nums[i] == val:
63+
nums[i] = nums[length - 1]
64+
length -= 1
65+
else:
66+
i += 1
67+
return length
2968

30-
# V3
31-
class Solution:
69+
# V2
70+
# Time: O(n)
71+
# Space: O(1)
72+
class Solution(object):
3273
# @param A a list of integers
3374
# @param elem an integer, value need to be removed
3475
# @return an integer
@@ -40,8 +81,4 @@ def removeElement(self, A, elem):
4081
last -= 1
4182
else:
4283
i += 1
43-
return last + 1
44-
45-
46-
47-
84+
return last + 1

0 commit comments

Comments
 (0)