본문 바로가기

코딩 테스트/프로그래머스

[Level 1, C++] 문자열 내 마음대로 정렬하기

728x90
반응형

 

sort 함수를 이용하는데 정렬 기준을 내가 만들면 쉽게 만들 수 있다.


#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int i;

bool compare(string s1, string s2) {
    if(s1[i] == s2[i]) {
        if(s1 < s2) return true;
        else return false;
    }
    else if(s1[i] < s2[i]){
        return true;
    }
    else return false;
}

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

 

728x90
반응형