-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRemove-Element.py
38 lines (36 loc) · 974 Bytes
/
Remove-Element.py
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
#! -*- coding=utf-8 -*-
# Date: 2019-11-18
# Author: Bryce
######Python Solution 1:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
res = 0
k = 0
n = len(nums)
if n == 0:
return res
for i in range(n):
if nums[i] != val:
nums[k] = nums[i]
k+=1
res = k
return res
######Python Solution 2:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
j=len(nums)
for i in range(j-1,-1,-1): ## 倒序
if nums[i]==val:
nums.pop(i) ###pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
return len(nums)