[프로그래머스] 같은 숫자는 싫어 ⭐

Date:     Updated:

카테고리:

태그:

난이도

📜문제

image image

🌞풀이

1. 전체 비교를 통한 풀이

#include <vector>
#include <iostream>

using namespace std;

vector<int> solution(vector<int> arr)
{
    vector<int> answer;

    for(int i =0; i< arr.size(); i++)
    {
        if(arr[i] != arr[i+1]) answer.push_back(arr[i]);
    }
    if(arr[arr.size()-1] == 0) answer.push_back(0);

    return answer;
}

2. Unique 함수를 이용한 풀이

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr)
{
    vector<int> answer;

    arr.erase(unique(arr.begin(), arr.end()), arr.end());
    answer = arr;

    return answer;
}

맨 위로 이동하기

Programmers 카테고리 내 다른 글 보러가기

댓글남기기