27 swap the ele
This commit is contained in:
parent
757193083b
commit
a295356ced
18
27-240525-pass/main.py
Normal file
18
27-240525-pass/main.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
class Solution:
|
||||||
|
def removeElement(self, nums: list[int], val: int) -> int:
|
||||||
|
cnt = 0
|
||||||
|
for num in nums:
|
||||||
|
if num == val: cnt+=1
|
||||||
|
for i in range(len(nums) - cnt):
|
||||||
|
if nums[i] == val:
|
||||||
|
for j in range(len(nums) - 1, 0, -1):
|
||||||
|
if nums[j] != val:
|
||||||
|
nums[i], nums[j] = (nums[j], nums[i])
|
||||||
|
print(nums)
|
||||||
|
return len(nums) - cnt
|
||||||
|
|
||||||
|
nums = [3, 2, 2, 3]
|
||||||
|
val = 3
|
||||||
|
sol = Solution()
|
||||||
|
print(sol.removeElement(nums, val))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user