8.15 [LeetCode] 179 Largest Number

[LeetCode 179] Largest Number

COMMENTS

Question

link

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.

Show Tags

Sort

Analysis

Please refer to [[ItInt5] Numbers Concatenation Max (Largest Number)(/blog/2014/08/17/Numbers-Concatenation-Max/).

Solution

Note that we can not override Comparator(Integer). So we must first convert int to string then sort.

Code

public class Solution {
    public String largestNumber(int[] nums) {
        if(nums == null || nums.length == 0)
            return "0";
        String[] str = new String[nums.length];
        for(int i = 0; i < nums.length; i++)
            str[i] = Integer.toString(nums[i]);
        Arrays.sort(str, new desecComp());
        StringBuilder sb = new StringBuilder();
        for(String s : str){

            if(sb.toString().length() == 0 && s.equals("0")) // use str.equals(str2) instead of str == str2 will be more stable;
                continue; // omit 0s in the beginning.
            else
                sb.append(s);
        }
        if(sb.toString().equals(""))
            return "0";
        return sb.toString();
    }

    class desecComp implements Comparator<String> { // do not forget "<String>" and this class is in the whole class
        public int compare(String s1, String s2) { // the return type is int!!! and the function name is compare not class name
            String a = s1 + s2;
            String b = s2 + s1;
            return b.compareTo(a); // descending order so we use b To a
        }
    }
}
时间: 2024-10-12 20:19:50

8.15 [LeetCode] 179 Largest Number的相关文章

leetCode 179. Largest Number 字符串排序 | Medium

179. 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 st

[Lintcode]184. Largest Number/[Leetcode]179. Largest Number

184. Largest Number/179. Largest Number 本题难度: Medium Topic: Greedy Description Largest Number 中文English Given a list of non negative integers, arrange them such that they form the largest number. Example Given [1, 20, 23, 4, 8], the largest formed nu

[LeetCode] 179. 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 i

Java 特定规则排序-LeetCode 179 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 i

leetcode[179]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 i

Java for LeetCode 179 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 i

[LeetCode] 179. Largest Number Java

题目: 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

LeetCode 179. Largest Number(排序)

题目 题意:给你一个数组,让你把数组里的元素组合起来,组合成的新数字最大 题解:把数字给排个序,但是排序的标准是啥呢?两个数字孰大孰小呢?判断标准就是两个数字分别前后组合,得出的数字哪个大,则前面的那个数字就大. 一开始我的思路错了,根据数字每个位上的数字判断,这样反而是变得更加复杂了! class Solution { public: string largestNumber(vector<int>& nums) { vector<string> strs; for(in

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,