[Python] Leetcode 27. Remove Element
Leetcode 27 Remove Element
문제
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums. Return k. Custom Judge:
풀이
- reader와 writer 2개의 포인터를 정의하고 투포인터로 풀 수 있음.
- Time Complexity O(n): i, j 최대 2n 씩 순회
- Complexity O(1)
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0 # writer
for j in range(len(nums)): # reader
if nums[j] != val:
nums[i] = nums[j]
i += 1
return i