728x90
난이도 : Easy
nums라는 리스트에서 val 값을 전부 제거하고, 리스트에 남은 요소가 몇 개인지 return하는 문제
단, nums만 수정할 것
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
a = 0
for i in range(len(nums)):
if nums[i] != val:
nums[a] = nums[i]
a += 1
return a
- for문에서 요소 하나씩 val와 같은지 확인
- val와 다른 요소들은 하나씩 자리를 바꿔버리고, 바뀐 수 a를 return
Runtime : 0ms
Memory : 17.91MB(14.24%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Python] 26. Remove Duplicates from Sorted Array (0) | 2025.01.20 |
---|---|
[Python] 383. Ransom Note (0) | 2025.01.19 |
[Python] 88. Merge Sorted Array (0) | 2025.01.17 |
[Python] 1089. Duplicate Zeros (0) | 2025.01.16 |
[Python] 1769. Minimum Number of Operations to Move All Balls to Each Box (0) | 2025.01.15 |