본문 바로가기
Problem-solving/프로그래머스

프로그래머스 - 문자열 내 마음대로 정렬하기 (C++)

by taehee.kim.dev 2020. 3. 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
 
 
 
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int N;
 
bool compare(string a, string b){
    
    if(a[N] != b[N])
        return a[N] < b[N];
    else{
        return a < b;
    }
}
 
vector<string> solution(vector<string> strings, int n) {
 
    N = n;
 
    sort(strings.begin(), strings.end(), compare);
 
    return strings;
}
 
 
 
int main(void){
    ios_base :: sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
    
    vector<string> strings{"abce""abcd""cdx"};
    int n = 2;
 
    vector<string> answer = solution(strings, n);
 
    for(int i = 0; i < answer.size(); i++){
        cout<<answer[i]<<" ";
    }
 
    return 0;
}
cs

댓글