프로그래머스 | 가장 큰 수 (정렬)
- 가장 큰 수
-
darklight
sublimevimemacs
C#
문제 설명
0 또는 양의 정수가 주어졌을 때, 정수를 이어 붙여 만들 수 있는 가장 큰 수를 알아내 주세요.
예를 들어, 주어진 정수가 [6, 10, 2]라면 [6102, 6210, 1062, 1026, 2610, 2106]를 만들 수 있고, 이중 가장 큰 수는 6210입니다.
0 또는 양의 정수가 담긴 배열 numbers가 매개변수로 주어질 때, 순서를 재배치하여 만들 수 있는 가장 큰 수를 문자열로 바꾸어 return 하도록 solution 함수를 작성해주세요.
제한 사항
- numbers의 길이는 1 이상 100,000 이하입니다.
- numbers의 원소는 0 이상 1,000 이하입니다.
- 정답이 너무 클 수 있으니 문자열로 바꾸어 return 합니다.
입출력 예
numbersreturn
[6, 10, 2] | 6210 |
[3, 30, 34, 5, 9] | 9534330 |
핵심 :
정렬
문자열 결합시 StringBuilder사용
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _08
{
class Program
{
static void Main(string[] args)
{
var s = new Solution();
//int[] numbers = {6,10,2};
//int[] numbers = {3,30,34,5,9};
int[] numbers = {0};
var result = s.solution(numbers);
Console.WriteLine(result);
}
}
public class Solution {
public string solution(int[] numbers) {
string answer = "";
//문자열로 더하면 3번에서 실패 뜸 (메모리)
StringBuilder sb = new StringBuilder();
List<string> list = new List<string>();
}
foreach(var str in list){
}
answer = sb.ToString();
answer = (answer[0] == '0') ? "0" : answer;
return answer;
}
}
}
|
다른사람 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public string solution(int[] numbers)
{
{
string XY = x.ToString() + y.ToString();
string YX = y.ToString() + x.ToString();
return YX.CompareTo(XY);
});
else return string.Join("", numbers);
}
}
|
List.Sort Method (System.Collections.Generic)
지정된 또는 기본 구현 또는 제공된 대리자를 사용하여 의 요소 또는 요소의 일부를 정렬하여 목록 요소를 비교합니다.Sorts the elements or a portion of the elements in the using either the specified or default implementation or a provided delegate to compare list elements.
docs.microsoft.com
https://docs.microsoft.com/ko-kr/dotnet/api/system.comparison-1?view=netframework-4.8
Comparison Delegate (System)
형식이 같은 두 개체를 비교하는 메서드를 나타냅니다.Represents the method that compares two objects of the same type.
docs.microsoft.com