Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1fb8518

Browse files
authoredOct 17, 2018
Add files via upload
1 parent 3fd4b81 commit 1fb8518

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

‎Remove Element/Remove_Element.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 第一种思路,利用python
2+
# 36ms 99.79%
3+
class Solution:
4+
def removeElement(self, nums, val):
5+
"""
6+
:type nums: List[int]
7+
:type val: int
8+
:rtype: int
9+
"""
10+
while val in nums:
11+
nums.remove(val)
12+
return len(nums)
13+
14+
# 第二种思路,使用双指针法,控制程序的时间复杂度为线性
15+
# 40ms 77.81%
16+
class Solution:
17+
def removeElement(self, nums, val):
18+
"""
19+
:type nums: List[int]
20+
:type val: int
21+
:rtype: int
22+
"""
23+
i, j = 0, len(nums) - 1
24+
while i <= j :
25+
if nums[i] == val:
26+
nums[i], nums[j] = nums[j], nums[i]
27+
j -= 1
28+
else:
29+
i += 1
30+
return j + 1

0 commit comments

Comments
 (0)
Please sign in to comment.