• LeetCode Largest Number


    Given a list of non negative integers, arrange them such that they form the largest number.

    For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

    Note: The result may be very large, so you need to return a string instead of an integer.

    Credits:
    Special thanks to @ts for adding this problem and creating all test cases.

     1 class StringComparator implements Comparator<String> {
     2 
     3     @Override
     4     public int compare(String a, String b) {
     5         if (a.length() == b.length()) {
     6             return b.compareTo(a);
     7         } else {
     8             String ab = a + b;
     9             String ba = b + a;
    10             return ba.compareTo(ab);
    11         }
    12     }
    13 }
    14 public class Solution {
    15     public String largestNumber(int[] num) {
    16         ArrayList<String> list = new ArrayList<String>(num.length);
    17         for (int i = 0; i < num.length; i++) {
    18             list.add(String.valueOf(num[i]));
    19         }
    20         Collections.sort(list,new StringComparator());
    21         String result = "";
    22         for (String s : list) {
    23             result += s;
    24         }
    25         if(result.charAt(0)=='0') return "0";
    26         return result;
    27     }
    28 }
  • 相关阅读:
    汽车文化【1196】
    西方经济学
    计算机组成原理【0013】
    C语言程序设计【0039】
    教育学【0405】
    管理学[9080]
    专业英语【0089】
    计算机基础1056
    letcode每日一题-上升下降字符串
    Flowable学习-flowable
  • 原文地址:https://www.cnblogs.com/birdhack/p/4225190.html
Copyright © 2020-2023  润新知