1 Star 0 Fork 0

agony/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
179.LargestNumber.cs 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
Kai Yang 提交于 2015-06-23 23:10 . 179. Largest Number
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Comparer: IComparer<string>
{
public int Compare(string left, string right)
{
return Compare(left, right, 0, 0);
}
private int Compare(string left, string right, int lBegin, int rBegin)
{
var len = Math.Min(left.Length - lBegin, right.Length - rBegin);
for (var i = 0; i < len; ++i)
{
if (left[lBegin + i] != right[rBegin + i])
{
return left[lBegin + i] < right[rBegin + i] ? -1 : 1;
}
}
if (left.Length - lBegin == right.Length - rBegin)
{
return 0;
}
if (left.Length - lBegin > right.Length - rBegin)
{
return Compare(left, right, lBegin + len, rBegin);
}
else
{
return Compare(left, right, lBegin, rBegin + len);
}
}
}
public class Solution {
public string LargestNumber(int[] nums) {
var sb = new StringBuilder();
var strs = nums.Select(n => n.ToString(CultureInfo.InvariantCulture)).OrderByDescending(s => s, new Comparer());
var nonZeroOccurred = false;
foreach (var str in strs)
{
if (!nonZeroOccurred && str == "0") continue;
sb.Append(str);
nonZeroOccurred = true;
}
return sb.Length == 0 ? "0" : sb.ToString();
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/agony2020/LeetCode.git
git@gitee.com:agony2020/LeetCode.git
agony2020
LeetCode
LeetCode
master

搜索帮助