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. 杭电1133 排队买票 catalan

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. a label can only be part of statement and a declaratioin is not a statement

    参考资料: https://stackoverflow.com/questions/18496282/why-do-i-get-a-label-can-only-be-part-of-a-statem ...

  3. [规则原则定理]规则原则定理章1CAP原则

    CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可兼得 分布式系 ...

  4. mybatis高级映射-一对多

    订单(一)和(多)订单明细 数据库结构如下所示[演示数据,真实表比这复杂得多] order表 订单明细表 xml映射表 <resultMap type="xxx.order" ...

  5. 提升HTML5的性能体验系列之五 webview启动速度优化及事件顺序解析

    webview加载时有5个事件.触发顺序为loading.titleUpdate.rendering.rendered.loaded.webview开始载入页面时触发loading,载入过程中如果&l ...

  6. 关于ueditor 在struts2 中 上传图片 ,未找到上传文件 问题的解决方法

    问题原因: ueditor 上传图片需请求imageUp.jsp文件,struts2 自带的拦截器(/*)把所有请求的文件都做了处理,所以导致无法上传图片. 解决方法: 方法一:自定义拦截器,让它在请 ...

  7. Windows事件日志报表 怎样备份数据库?

  8. 2018.12.08 codeforces 914D. Bash and a Tough Math Puzzle(线段树)

    传送门 线段树辣鸡题. 题意简述:给出一个序列,支持修改其中一个数,以及在允许自行修改某个数的情况下询问区间[l,r][l,r][l,r]的gcdgcdgcd是否可能等于一个给定的数. 看完题就感觉是 ...

  9. tflite笔记

    固化模型 方法一:freeze_graph方法 把tf.train.write_graph()生成的pb文件与tf.train.saver()生成的chkp文件固化之后重新生成一个pb文件 with ...

  10. OSI七层模型和TCP/IP四层模型

    1)网络层负责点到点的传输(这里的“点”指主机或路由器),而传输层负责端到端的传输(这里的“端”指应用进程) 2)ARP协议介于数据链路层和网络层之间(IPv4专有,IPv6的地址映射功能在ICMPv ...