-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path27.py
34 lines (30 loc) · 806 Bytes
/
27.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
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if len(nums)==0:
return 0
if len(nums)==1 and nums[0]==val:
return 0
if len(nums)==1 and nums[0]!=val:
return 1
i = 0
j = len(nums)-1
while i < j:
while(i < len(nums) and nums[i]!=val):
i+=1
while(j >= 0 and nums[j]==val):
j-=1
if (i<j):
nums[i],nums[j]=nums[j],nums[i]
k = len(nums)-1
while(k>=0 and nums[k]==val):
k-=1
return k+1
if __name__=='__main__':
s = Solution()
nums=[0,1,2,2,3,0,4,2]
print(s.removeElement(nums,2))