-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy path0179-LargestNumber.cs
More file actions
32 lines (26 loc) · 914 Bytes
/
0179-LargestNumber.cs
File metadata and controls
32 lines (26 loc) · 914 Bytes
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
//-----------------------------------------------------------------------------
// Runtime: 124ms
// Memory Usage: 27.3 MB
// Link: https://leetcode.com/submissions/detail/400464395/
//-----------------------------------------------------------------------------
using System;
using System.Linq;
using System.Text;
namespace LeetCode
{
public class _0179_LargestNumber
{
public string LargestNumber(int[] nums)
{
if (nums.Length == 0) return string.Empty;
if (nums.Length == 1) return nums[0].ToString();
var numsStr = nums.Select(num => num.ToString()).ToArray();
Array.Sort(numsStr, (a, b) => (b + a).CompareTo(a + b));
if (numsStr[0] == "0") return "0";
var sb = new StringBuilder();
foreach (var str in numsStr)
sb.Append(str);
return sb.ToString();
}
}
}