Post

leetcode(리트코드)26-Remove Duplicates from Sorted Array

leetcode Array intro - Remove Duplicates from Sorted Array 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

leetcode의 Array introduction에 있는 문제입니다.
eazy난이도의 문제입니다.


4. 문제 해석

  • 중복되는 수를 제거하여 주어진 배열을 바꾸는 문제입니다.
    문제에서 새 배열을 만들지 말고 O(1)로 푸는 것을 권장하였습니다.

5. code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int left =0;
        for(size_t i=0;i<nums.size();++i)
        {
            nums[left++] = nums[i];
            while(i+1< nums.size() && nums[i] == nums[i+1])
            {
                ++i;
            }
        }
        return left;
    }
};

6. 결과 및 후기, 개선점

시간

굳이 개선할 점 없음.

This post is licensed under CC BY 4.0 by the author.