-
30. 정수 내림차순으로 배치하기코딩 테스트/Level 1 2019. 10. 28. 14:55반응형
https://programmers.co.kr/learn/courses/30/lessons/12933
파이썬
def solution(n): return int(''.join(sorted(str(n), reverse=True)))
자바스크립트
function solution(n) { return Number(String(n).split('').sort((a, b) => a <= b ? 1 : -1).join('')) }
자바
import java.util.Arrays; class Solution { public long solution(long n) { var numbers = String.valueOf(n).split(""); Arrays.sort(numbers); String temp = ""; for (var each : numbers) { temp = each + temp; } return Long.valueOf(temp); } }
import java.util.*; class Solution { public long solution(long n) { return Long.parseLong( Arrays.stream(String.valueOf(n).split("")) .sorted(Comparator.reverseOrder()) .reduce("", String::concat) ); } }
코틀린
class Solution { fun solution(n: Long): Long = n.toString().toCharArray().sortedDescending().joinToString("").toLong() }
고
import "sort" func solution(n int64) (result int64) { var temp []int for n > 0 { temp = append(temp, int(n%10)) n /= 10 } sort.Sort(sort.Reverse(sort.IntSlice(temp))) for _, v := range temp{ result = result * 10 + int64(v) } return }
C#
using System; using System.Linq; public class Solution { public long solution(long n) { char[] temp = n.ToString().ToArray(); Array.Sort(temp); Array.Reverse(temp); return long.Parse(temp); } }
using System; using System.Linq; public class Solution { public long solution(long n) => long.Parse(n.ToString().OrderBy(x => x).Reverse().ToArray()); }
반응형