`
hcx2013
  • 浏览: 82113 次
社区版块
存档分类
最新评论

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.

 

public class Solution {
    public String largestNumber(int[] nums) {
        int len = nums.length;
        if (len < 1) {
        	return "";
        }
        String[] strs = new String[len];
        for (int i = 0; i < len; i++) {
        	strs[i] = String.valueOf(nums[i]);
		}
        Arrays.sort(strs, new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return (o1 + o2).compareTo(o2 + o1);
			}
		});
        StringBuffer res = new StringBuffer();
        for (int i = len-1; i >= 0; i--) {
        	res.append(strs[i]);
		}
        int i = 0;
        while (i < len && res.charAt(i) == '0') {
        	i++;
        }
        if (i == len) {
        	return "0";
        }
        return res.substring(i).toString();
    }
}

 

0
3
分享到:
评论

相关推荐

    题解:ABC273C - (K+1)-th Largest Number

    代码

    c程序设计第四版课后习题答案

    习题答案,过程详细,仅供参考。 摘录文档片段如下: 1-6. #include int main() {int a,b,c,max; printf("please input a,b,c:\n");... printf("The largest number is %d\n",max); return 0; }

    JS中取二维数组中最大值的方法汇总

    在JavaScript中可以通过内置的 Math.max() 的最大值,但是要从多重数组中取出最大值,还是有一定的难度。 问题描述 假设你有一个数组,而且这个数组中...var largestNumber = 0; // 创建第二个变量,存储最大的数 //

    sfs.rar_FH DS_The Show_dw_dw vhdl

    MES2 DB 0AH,0DH, The largest number is:$ NUMB DB 0D9H,07H,8BH,0C5H,0EBH,04H,9DH,0F9H DDATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DDATA START: MOV AX,DDATA MOV DS,AX MOV SI,OFFSET NUMB MOV CX,...

    c程序设计第四版谭浩强完整课后答案

    #include &lt;stdio.h&gt; int main ( ) { printf ("**************************\n\n"); printf(" Very Good!\n\n"); ... printf ("**************... printf("The largest number is %d\n",max); return 0; }

    cpp-算法精粹

    Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II Search a 2D Matrix Search a 2D Matrix II Find Minimum in Rotated ...

    北京邮电大学 离散数学上机第1001题

    Description Now, there are a list of N numbers. Your job is to find the Mth largest number X. (there are M-1 numbers &gt;= X)

    java-assignment-return-values-cmadisonne:GitHub Classroom创建的java-assignment-return-values-cmadisonne

    println(“ The largest number is : “ + largestNumber(number1, number2)) 创建一个largestNumber方法,该方法将返回传入的2中最大的数字匹配密码检查器给定一个主要功能: Scanner scanInput = new Scanner ( ...

    Pro TypeScript with Steve Fenton 2018 2版

    The ECMAScript 5 specification forms the basis of TypeScript and supplies the largest number of features in the language. Subsequent versions of the ECMAScript specification are rolled into ...

    基于Linux系统下的邮件服务器.doc

    Use the largest number of mail system in enterprise. mail system based on Postfix/Sendmail is in need of a strong technical force to realize, but performance can reach very high, but very good ...

    基于linux系统下的邮件服务器(1).doc

    Use the largest number of mail system in enterprise. mail system based on Postfix/Sendmail is in need of a strong technical force to realize, but performance can reach very high, but very good ...

    LeetCode最全代码

    The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...

    functions2

    if (n1 &gt;= n2 && n1 &gt;= n3) printf("%.2f is the largest number.", n1); if (n2 &gt;= n1 && n2 &gt;= n3) printf("%.2f is the largest number.", n2); if (n3 &gt;= n1 && n3 &gt;= n2) printf("%.2f is the largest number....

    DanColle-Samsung-Note-7

    对于Steam用户, /steamapps/common/World of Warships/bin/[largest number]/res_mods/ content复制到/steamapps/common/World of Warships/bin/[largest number]/res_mods/ 使用方法 WG游戏中心用户请把content放...

    DanColle-Excalibur

    [仅限美联社] Fate Excalibur / EX咖喱棒 AP外壳模型修改安装对于WGC用户,将content复制到[WoWS folder]/bin/[largest number]/res_mods/对于Steam用户, /steamapps/common/World of Warships/bin/[largest number...

    Proxy Pattern Informatization Research Based On SaaS

    This approach has the lowest hardware and backup costs, but it has the highest sharing, because it allows you to serve the largest number of users per database server. Some proxy industry like ...

    1234_TheAnswer_printlargestnumber_

    You should try this. this is very clear answer. The question is about printing the kth largest number in the array

    ARP-Takao-Atago-Blue

    由SEA集团主办安装对于WGC用户,将gui PnFMods和PnFModsLoader.py复制到[WoWS folder]/bin/[largest number]/res_mods/对于Steam用户, PnFMods gui PnFMods和PnFModsLoader.py复制到/steamapps/common/World of ...

    XJBMG-Ra-Cailum-Shield

    对于Steam用户, PnFMods和PnFModsLoader.py复制到/steamapps/common/World of Warships/bin/[largest number]/res_mods/ 使用方法 WG游戏中心用户请把PnFMods和PnFModsLoader.py放到[游戏安装目录]/bin/[数字最大...

Global site tag (gtag.js) - Google Analytics