We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9603026 commit 7159bf1Copy full SHA for 7159bf1
python/0027-remove-element.py
@@ -1,10 +1,8 @@
1
-class Solution(object):
2
- def removeElement(self, nums, val):
3
- """
4
- :type nums: List[int]
5
- :type val: int
6
- :rtype: int
7
8
- for i in range(nums.count(val)): # loop how many val is in the list
9
- nums.remove(val) # remove each val one by one
10
- return len(nums) # return len of nums
+class Solution:
+ def removeElement(self, nums: List[int], val: int) -> int:
+ k = 0
+ for i in range(len(nums)):
+ if nums[i] != val:
+ nums[k] = nums[i]
+ k += 1
+ return k
0 commit comments