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;
}

[leed code 179] Largest Number的更多相关文章

  1. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  2. [LeetCode] 179. Largest Number 最大组合数

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

  3. Java 特定规则排序-LeetCode 179 Largest Number

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

  4. leetcode 179. Largest Number 求最大组合数 ---------- java

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

  5. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  6. Java for LeetCode 179 Largest Number

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

  7. 179. Largest Number -- 数字字符串比较大小

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

  8. 179. Largest Number

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

  9. [LeetCode] 179. Largest Number 解题思路

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

随机推荐

  1. C++/CLI学习入门

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxIAAAFlCAYAAAB/fN6bAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw

  2. Linux renew ip command

    $ sudo dhclient -r    //release ip 释放IP$ sudo dhclient       //获取IP Now obtain fresh IP:$ sudo dhcli ...

  3. vc项目中加载多个lib遇到的问题

    一个VC项目中 在网络加密 json解析等方面  加载了多个第三方库和文件 boost cryptpp rapidjson  mysql的连接池等等 在使用mysql++的时候 多次报错 LNK 20 ...

  4. 这一周~&&html+css的学习感悟

    一周一周过的很快,这个礼拜的学习状态并不是很好,好像每个月都有那么几天学习状态不怎么样.不知道是懈怠了还是怎么了…… 没有辜负上周一开始的目标,4.6号之前我就糊好了篇论文交了上去,不知道结果如何,希 ...

  5. 如何在Eclipse下安装SVN插件——subclipse

    如何在Eclipse下安装SVN插件——subclipse | 浏览:2799 | 更新:2014-09-20 22:39 1 2 3 4 5 6 分步阅读 版本控制是开发人员必不可少的工具,而SVN ...

  6. javascript声明对象时 带var和不带var的区别

    2015/11/25补充: 关于变量声明这里有详细的解释: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Stat ...

  7. Globalization and accessibility for tile and toast notifications (Windows Store apps)

    http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...

  8. Reading CLR via c# 4th Edition

    In fact, at runtime, the CLR has no idea which programming language the developer used for thesource ...

  9. Java第14章笔记

    Java 中无参无返回值和带参带返回值习题 编写一个 Java 程序,实现输出学生年龄的最大值 要求: 1. 要求通过定义无参带返回值的方法来实现,返回值为最大年龄 2. 方法中将​学生年龄保存在数组 ...

  10. mysql之零碎知识

    一 视图 什么是视图:视图就是一张虚拟表.方便查看. 创建视图:create view 起名 as sql语句 #两张有关系的表 mysql> select * from course; +-- ...