문제 설명
문자열로 구성된 리스트 strings와, 정수 n이 주어졌을 때, 각 문자열의 인덱스 n번째 글자를 기준으로 오름차순 정렬하려 합니다. 예를 들어 strings가 ["sun", "bed", "car"]이고 n이 1이면 각 단어의 인덱스 1의 문자 "u", "e", "a"로 strings를 정렬합니다.제한 사항
strings는 길이 1 이상, 50이하인 배열입니다.
strings의 원소는 소문자 알파벳으로 이루어져 있습니다.
strings의 원소는 길이 1 이상, 100이하인 문자열입니다.
모든 strings의 원소의 길이는 n보다 큽니다.
인덱스 1의 문자가 같은 문자열이 여럿 일 경우, 사전순으로 앞선 문자열이 앞쪽에 위치합니다.입출력 예
strings
["sun", "bed", "car"]
n 1
return ["car", "bed", "sun"]strings ["abce", "abcd", "cdx"]
n 2
return ["abcd", "abce", "cdx"]
#include <string> #include <vector> #include <algorithm> using namespace std; vector<string> solution(vector<string> strings, int n) { vector<string> answer; sort(strings.begin(), strings.end(), [&n](const string& a, const string& b ){ if (a[n] == b[n]) return a < b; return a[n] < b[n]; }); return answer = strings; }
문제 설명
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.제한 사항
n은 1이상 8000000000 이하인 자연수입니다.입출력 예
n return
118372 873211
#include <string> #include <vector> #include <algorithm> using namespace std; long long solution(long long n) { long long answer = 0; vector<int> digit; while (n != 0) { digit.push_back(n % 10); n /= 10; } sort(digit.begin(), digit.end(), [](const int& a, const int& b){ return a > b; }); long long times = 1; for (auto it = digit.rbegin(); it != digit.rend(); ++it) { answer += *it * times; times*=10; } return answer; }