返回介绍

solution / 0100-0199 / 0179.Largest Number / README_EN

发布于 2024-06-17 01:04:03 字数 3938 浏览 0 评论 0 收藏 0

179. Largest Number

中文文档

Description

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.

Since the result may be very large, so you need to return a string instead of an integer.

 

Example 1:

Input: nums = [10,2]
Output: "210"

Example 2:

Input: nums = [3,30,34,5,9]
Output: "9534330"

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def largestNumber(self, nums: List[int]) -> str:
    nums = [str(v) for v in nums]
    nums.sort(key=cmp_to_key(lambda a, b: 1 if a + b < b + a else -1))
    return "0" if nums[0] == "0" else "".join(nums)
class Solution {
  public String largestNumber(int[] nums) {
    List<String> vs = new ArrayList<>();
    for (int v : nums) {
      vs.add(v + "");
    }
    vs.sort((a, b) -> (b + a).compareTo(a + b));
    if ("0".equals(vs.get(0))) {
      return "0";
    }
    return String.join("", vs);
  }
}
class Solution {
public:
  string largestNumber(vector<int>& nums) {
    vector<string> vs;
    for (int v : nums) vs.push_back(to_string(v));
    sort(vs.begin(), vs.end(), [](string& a, string& b) {
      return a + b > b + a;
    });
    if (vs[0] == "0") return "0";
    string ans;
    for (string v : vs) ans += v;
    return ans;
  }
};
func largestNumber(nums []int) string {
  vs := make([]string, len(nums))
  for i, v := range nums {
    vs[i] = strconv.Itoa(v)
  }
  sort.Slice(vs, func(i, j int) bool {
    return vs[i]+vs[j] > vs[j]+vs[i]
  })
  if vs[0] == "0" {
    return "0"
  }
  return strings.Join(vs, "")
}
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();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文