[leed code 179] Larges Number

1 题目

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.

2 方法

刚开始就误入歧途,准备按字符来比较,结果逻辑各种复杂,网上查了查,结果发现直接比较两个连起来的字符串要简单的多。比如字符串a和字符串b,比较ab和ba的大小就搞定了。

3 代码

     public String largestNumber(int[] num){
            String[] strArray = new String[num.length];
            for (int i = 0; i < num.length; i++) {
                strArray[i] = Integer.toString(num[i]);
            }
            strArray = this.anotherSort(strArray);//(strArray);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < strArray.length; i++) {
                sb.append(strArray[i]);
            }
            String out = sb.toString();
            if(out.charAt(0) == ‘0‘) return "0";
            return out;
        }
    public static String[] anotherSort(String[] strSort) {
        String temper = "";
        for (int i = 0; i < strSort.length; i++) {
            for (int j = i + 1; j < strSort.length; j++) {
                String add1 = strSort[i] + strSort[j];
                String add2 = strSort[j] + strSort[i];
                int out = add1.compareTo(add2);
                if (out < 0) {
                    temper = strSort[i];
                    strSort[i] = strSort[j];
                    strSort[j] = temper;
                }
            }
        }
        return strSort;
    }    
时间: 2024-12-24 21:59:23

[leed code 179] Larges Number的相关文章

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 ver

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

No.179 Largest Number

No.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

Error Code: 1318. Incorrect number of arguments for PROCEDURE student.new_procedure; expected 0, got

1.错误描述 13:58:20 call new_procedure('2000','zhangsan') Error Code: 1318. Incorrect number of arguments for PROCEDURE student.new_procedure; expected 0, got 2 0.000 sec 2.错误原因 CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`() BEGIN set @a=1

Error Code: 1318. Incorrect number of arguments for PROCEDURE company.new_procedure; expected 2, got

1.错误描述 20:27:34 call new_procedure(20150112) Error Code: 1318. Incorrect number of arguments for PROCEDURE company.new_procedure; expected 2, got 1 0.000 sec 2.错误原因 CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`(in `departId` int,out `nu

[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

Error Code 1318 Incorrect number of arguments for PROCEDUR

1.错误描述 13:58:20 call new_procedure('2000','zhangsan') Error Code: 1318. Incorrect number of arguments for PROCEDURE student.new_procedure; expected 0, got 2 0.000 sec 2.错误原因 CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`() BEGIN set @a=1

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

【LeetCode】179. Largest Number

Description: For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Analysis: The problem can be solved by sorting. It's reallllllllllllllllllllllllllllllllllllllllllly a tallent and brilliant idea to give a compare function like