diesuki4 2023. 4. 14. 07:08
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);
    });
}