C++:: 프로그래머스 < 문자열 내 마음대로 정렬하기 >

jahlee·2023년 8월 9일
0

프로그래머스_Lv.1

목록 보기
71/75
post-thumbnail

문제에서 주어진 조건대로 idx번째의 문자를 기준으로 오름차순 정렬하면 된다. 같다면 사전순.

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

int idx;

bool compare(string& a, string& b) {
    if (a[idx] == b[idx]) return a < b;
    return a[idx] < b[idx];
}

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

1개의 댓글

comment-user-thumbnail
2023년 8월 9일

훌륭한 글 감사드립니다.

답글 달기