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 |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 392. Is Subsequence (0) | 2023.06.19 |
---|---|
leetcode 13. Roman to Integer (1) | 2023.06.19 |
leetcode 744. Find Smallest Letter Greater Than Target (0) | 2023.06.09 |
백준 28074 모비스 (0) | 2023.06.08 |
백준 28097 모범생 포닉스 (0) | 2023.06.08 |
댓글