https://programmers.co.kr/learn/courses/30/lessons/12915

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int k;
bool cmp(string a, string b) {
    if (a[k] != b[k]) return a[k] < b[k]; //그대로 정렬
    else return a < b; // 문자가 같을 경우 사전 순 정렬
}
 
vector<string> solution(vector<string> strings, int n) {
    k = n;
    sort(strings.begin(), strings.end(), cmp);
 
    return strings;
    //return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

sort의 정렬 기준을 따로 설정할 수 있다는 것을 몰라서

생각보다 오래 걸렸던 것 같다.

sort함수의 3번째 인자로 정렬 기준을 잡을 수 있다.

+ Recent posts