Post

leetcode(리트코드)27-Remove Element

leetcode Array intro - Remove Element 문제입니다.

1. 문제

https://leetcode.com/problems/remove-element/


2. Input , Output


3. 분류 및 난이도

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


4. 문제 해석

  • val로 들어온 값을 새 배열을 만들지 않고 지워서 값을 리턴합니다. 다만, 지우고 남은 원소들을 앞으로 끄집어 내야합니다.

5. code

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

6. 결과 및 후기, 개선점

시간

굳이 개선할 점 없음.

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