🍞 Problem Solving/Programmers

[프로그래머스][Level1] 문자열 내 마음대로 정렬하기 c++

박빵이 2022. 9. 3. 13:57

풀이과정

전역 변수 N을 선언해줘서 n 인덱스 값을 넣는다.  
sort함수를 써서 cmp 조건에 따라 오름차순 정렬한다.

 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int N;
bool cmp(string a, string b){
    if(a[N] == b[N]) return a < b;
    else return a[N] < b[N];
}

vector<string> solution(vector<string> strings, int n) {
    vector<string> answer;
    N = n;
    sort(strings.begin(), strings.end(), cmp);
    answer = strings;
    return answer;
}