코딩 공부/Leetcode

[Python] 26. Remove Duplicates from Sorted Array

일하는 공학도 2025. 1. 20. 10:47
728x90

난이도 : Easy

겹치는 요소가 하나도 없게 nums 내부에서 제거할 것

 

ex 1

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]

 

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        k = 1
        for i in range(1, len(nums)):
            if nums[k] in nums[:k]:
                del nums[k]
                k -=1
            k += 1

nums[k]의 요소가 nums[0:k] 범위 안에 있으면 지우기

728x90

'코딩 공부 > Leetcode' 카테고리의 다른 글

[Python] 802. Find Eventual Safe States  (0) 2025.01.24
[Python] 1267. Count Servers that Communicate  (0) 2025.01.23
[Python] 383. Ransom Note  (0) 2025.01.19
[Python] 27. Remove Element  (0) 2025.01.18
[Python] 88. Merge Sorted Array  (0) 2025.01.17