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

수민이슈·2023년 4월 18일
0

[C++] 코딩테스트

목록 보기
19/116
post-thumbnail

🖊️ 문제

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

🖊️ 풀이

진짜 캐 간단한데..
왜 겁먹고 못풀었냐면
ㅋㅋ
전역변수로 선언해서 넣어준다는 생각을 전혀 못하고
어어.. compare 함수에 인자 어떻게 넣어주지..?
요러고 있었음 ㅠㅠ
제발요...

🖊️ 코드

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

using namespace std;

int N;

bool compare(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(), compare);
    answer = strings;
    
    return answer;
}

0개의 댓글