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. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  2. 通过修改Delphi 的 RTL,加快Delphi开发的应用程序速度和稳定性

    RT 具体见PDF 看了不后悔,只给高手准备的. 神呐,偶看看是虾米东东

  3. EXISTS 和 IN 的区别

    exists子句的用法 select * from 表1 where exists (select * from 表2 where 表1.列名=表2.列名); exists子句返回的结果并不是从数据库 ...

  4. Spring 循环引用(二)源码分析

    Spring 循环引用(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 循环引用相关文章: & ...

  5. Two Sum III - Data structure design LT170

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  6. 如何为终端用提供更快的解决方案?让IT技术员具备更高的效率?

  7. Django+Uwsgi+Nginx项目部署文档

    一.基本环境搭建 1)查看服务器 [root@Myjumpserver ~]# cat /etc/sysconfig/selinux SELINUX=disabled SELINUXTYPE=targ ...

  8. JavaScript的进阶篇

    一.Array对象.数组对象 1)创建数组对象 //Array 对象用于在单个的变量中存储多个值. //语法: //创建方式1: ,,]; //创建方式2: new Array(); // 创建数组时 ...

  9. matlab 设定坐标比例

    figure() u=-0.1:0.005:0.1; v=-0.1:0.005:0.1; [x,y]=meshgrid(u,v); z=sin(x-y)./abs(x)+abs(y); surf(x, ...

  10. mysql知识积累

    验证mysql工作状态 systemctl status mysql.service 启动 sudo systemctl start mysql 停止 service mysql stop 重启mys ...