C++)
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int index;
bool compare(string& str1, string& str2) {
// 비교하고자 하는 위치의 문자가 같다면
if(str1.at(index) == str2.at(index))
return str1 < str2; // 오름차순 정렬
// 아니라면
else
return str1.at(index) < str2.at(index); // 비교하는 문자 기준 오름차순 정렬
}
vector<string> solution(vector<string> strings, int n) {
index = n;
sort(strings.begin(), strings.end(), compare);
return strings;
}
Python)
def solution(strings, n):
strings.sort()
strings.sort(key=lambda st : st[n])
return strings'프로그래머스 > Level1' 카테고리의 다른 글
| [Level1] 비밀지도 (C++/Python) (0) | 2023.05.16 |
|---|---|
| [Level1] 크기가 작은 부분 문자열 (C++/Python) (0) | 2023.05.15 |
| [Level1] 숫자 문자열과 영단어 (C++) (0) | 2023.05.13 |
| [Level1] 다트 게임 (C++) (0) | 2023.05.06 |
| [Level1] 덧칠하기 (C++/Python) (0) | 2023.05.05 |