LeetCode 179. Largest Number(最大数)

原题网址:https://leetcode.com/problems/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.

方法:将数字按照字符串进行比较,当两个数字为包含关系的时候(例如数字80和806,则将它们连接起来比较,80806和80680,看谁大,谁就应该排在前面)。

public class Solution {
    public String largestNumber(int[] nums) {
        String[] strs = new String[nums.length];
        for(int i=0; i() {
            @Override
            public int compare(String s1, String s2) {
                if (s1.length() == s2.length()) return s2.compareTo(s1);
                return (s2+s1).compareTo(s1+s2);
            }
        });
        if ("0".equals(strs[0])) return "0";
        StringBuilder sb = new StringBuilder();
        for(int i=0; i


你可能感兴趣的:(数字,字符串,组合,最值,排列,前缀,分情况)