Level 2. 가장 큰 수
단순히 정렬만 해서는 안 된다.
- 단순 문자열 정렬 시, [3, 30] -> 303이 되기 때문이다.
비교할 두 문자열을 직접 붙여 비교해봐야 한다.
모든 수가 0인 경우를 주의해야 한다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
using namespace std;
string solution(vector<int> numbers)
{
sort(numbers.begin(), numbers.end(), [](int a, int b)
{
string A = to_string(a), B = to_string(b);
return A + B > B + A;
});
return accumulate(numbers.begin(), numbers.end(), string(), [](string s, int num)
{
return (s == "0" ? "" : s) + to_string(num);
});
}
'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글
Level 2. [3차] 파일명 정렬 (0) | 2023.04.16 |
---|---|
Level 2. 괄호 회전하기 (0) | 2023.04.15 |
Level 2. 카펫 (0) | 2023.04.13 |
Level 2. 2개 이하로 다른 비트 (0) | 2023.04.12 |
Level 2. 전화번호 목록 (0) | 2023.04.11 |