Problem Solving

97. Remove Duplicates from Sorted Array

굥깡 2023. 1. 19. 06:07
728x90

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

 

Remove Duplicates from Sorted Array - LeetCode

Remove Duplicates from Sorted Array - Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place [https://en.wikipedia.org/wiki/In-place_algorithm] such that each unique element appears only once. The relative order of the e

leetcode.com

array의 중복값을 제거하는 문제

class Solution:
    def removeDuplicates(self, nums: list[int]) -> int:
        count = 0
        for i, num in enumerate(nums):
            if nums.count(num) > 1:
                nums[i] = -101
                count = count + 1
        for i in range(count):
            nums.remove(-101)

따로 메모리를 할당할 수 없다는 점은 생각보다 큰 제한 조건이다

처음 풀 때는 시간초과 등으로 여러 번 리젝 됐는데, 다시 풀면서 결국 96. Remove Element과 비슷하게 풀게 됐다

사실 이렇게 풀면 안 됨, input에 -101이라는 특정한 값이 포함될 수 있기 때문