python-algorithm
leetcode 27. Remove Element
무적김두칠
2023. 6. 19. 00:06
Remove Element - LeetCode
Can you solve this real interview question? Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed. Then r
leetcode.com
1
2
3
4
5
6
7
8
9
|
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
val_cnt = nums.count(val)
for _ in range(val_cnt):
nums.remove(val)
nums.extend(['_']*val_cnt)
answer = len(nums) - val_cnt
return answer
|
cs |
반응형