Posts [프로그래머스] 같은 숫자는 싫어 / C++
Post
Cancel

[프로그래머스] 같은 숫자는 싫어 / C++


Contents



문제


문제를 보시려면 링크를 클릭해주세요.


풀이


vector를 이용해 연속해서 같은 값이 아닐 때만 answer에 담아주었습니다.

algorithm 헤더에 있는 unique 함수를 이용하면 아래 코드보다 더 간단하게 풀 수 있습니다.


소스 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <iostream>

using namespace std;

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

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

    return answer;
}



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

[OS 운영체제] 메모리 계층구조 (Memory Hierarchy)

[프로그래머스] 3진법 뒤집기 / C++

Comments powered by Disqus.