333. 가장 큰 수

아현·2021년 11월 12일
0

Algorithm

목록 보기
357/400
post-custom-banner

프로그래머스



1. Python


import functools

def comparator(a,b):
    t1 = a+b
    t2 = b+a
    return (int(t1) > int(t2)) - (int(t1) < int(t2)) 
    # t1이 크다면 1  // t2가 크다면 -1  //  같으면 0

def solution(numbers):
    n = [str(x) for x in numbers]
    n = sorted(n, key=functools.cmp_to_key(comparator),reverse=True)
    answer = str(int(''.join(n)))
    return answer



2. C++



#include <vector>
#include <string>
#include <algorithm>
using namespace std;
 
bool compare(string a, string b){
    return a + b > b + a;
}
 
string solution(vector<int> numbers) {
    string answer = "";
    vector<string> temp;
    for (int num : numbers)
        temp.push_back(to_string(num));
    
    sort(temp.begin(), temp.end(), compare);
    
    if (temp.at(0) == "0") return "0";
    
    for (auto num : temp)
        answer += num;
    
    return answer;
}



3. JavaScript


function solution(numbers) {
    
    var answer = numbers.map(c => c + ''). //Stirng(c)
    			sort((a,b) => (b + a) - (a + b)).join('');
    
    return answer[0] === '0'? '0' : answer;
}



profile
For the sake of someone who studies computer science
post-custom-banner

0개의 댓글