문제 설명
문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 [sun, bed, car]이고 n이 1이면 각 단어의 인덱스 1의 문자 u, e, a로 strings를 정렬합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <string> #include <vector> #include <algorithm> #include <iostream> #include "main.h" using namespace std; int num; bool compare(string a, string b) { //원소값이 같은 경우 if (a[num] == b[num]) { return a < b; } else { return a[num] < b[num]; } } vector<string> solution(vector<string> strings, int n) { vector<string> answer; answer = strings; num = n; sort(answer.begin(), answer.end(), compare); return answer; } | cs |
'알고리즘 문제' 카테고리의 다른 글
(C++) 자릿수 더하기 (0) | 2020.05.26 |
---|---|
(C++) 문자열 내림차순으로 배치하기 (0) | 2020.05.23 |
(C++) 문자열 다루기 기본 (0) | 2020.05.20 |
(C++) 짝수와 홀수 (0) | 2020.05.20 |
(C++)약수의 합 (0) | 2020.05.12 |