https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
이미 오름차순 정렬된 리스트가 주어지며, 해당 리스트의 중복원소를 모두 제거한 후 상대적인 정렬도는 유지해야 한다.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) < 2 :
return
initial = nums[0]
pointer =0
for num in nums :
if num > initial:
pointer += 1
nums[pointer] = num
initial = num
return(pointer+1)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
nums[:] = sorted(set(nums))
return len(nums)
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length < 2)
return nums.length;
int pointer = 1;
int num = nums[0];
for(int i=0; i < nums.length; i++){
if(nums[i] > num){
nums[pointer++] = nums[i];
num = nums[i];
}
}
return pointer;
}
LeetCode 는 단순 테케 통과보다 메모리와 런타임 효율을 고려하여 짜도록 설계되어서 꼼꼼히 공부하는데에 더 좋은 듯하다.
풀었던 것도 또 풀어보자.
Most of the classic interview questions have multiple solution approaches.
For the best practice result, we strongly advise you to go through this list at least a second time, or even better - a third time.