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.

分析:给定一个正整数数组,将数组的数字整合成一个最大的数字。

样例:① [3, 30, 34, 5, 9]->9534330

   ②[121,12]->12121

   ③[0,0]  -> 0

(1)将数组的数字组合,这隐含着一个大数问题,所以不可以直接用int或者long来保存结果,需要用到字符串(也可以用数组).

(2)字符串m与字符串n拼接,可以成为mn或者nm。要比较mn与nm的大小,不能直接用Arrays.sort()函数,这个函数排序后的结果为升序,重写Comparator 实现降序,才能组成最大的数字。

(3)字符串m与n的大小比较规则:当mn>nm时,m>n;

                  当mn=nm时,m=n;

                  当mn<nm时,m<n;

(4)字符串的比较规则:str1="abcda";str2="abcdb";当比较到第五个字符str1为a,str2为b  。a的ASCII码小于b的ASCII码。所以str1<str2

下面的代码 直接使用 Arrays.sort(com, new Comparator<Object>() { .....});在sort函数中重写 Comparator<Object>(),效率略低。跑完leetcode上的样例需要334ms。

public String largestNumber(int[] nums) {
if (nums == null)
return null;
int len = nums.length;
String[] com = new String[len];
int all0 = ;
for (int i = ; i < len; i++) {
if (nums[i] == ) {
all0++;
System.out.println("~~~" + all0);
}
com[i] = Integer.toString(nums[i]);
}
Arrays.sort(com, new Comparator<Object>() {
public int compare(Object obj1, Object obj2) {
String str1 = (String) obj1;
String str2 = (String) obj2;
String com1 = str1 + str2;
System.out.println("com1:" + com1);
String com2 = str2 + str1;
System.out.println("com2:" + com2);
if (com1.compareTo(com2) >= ) {
System.out.println("com1<com2");
return -;
} else {
System.out.println("com1>com2");
return ;
}
}
}); String ans = "";
for (int i = ; i < len; i++) {
ans += com[i];
}
if (all0 == len)
ans = "";
return ans;
}

做出以下改进 拍完样例的时间为120ms

Comparator<String> comp = new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
// reverse order here, so we can do append() later
}
};
Arrays.sort(com, comp);

【Leetcode】179. Largest Number的更多相关文章

  1. 【刷题-LeetCode】179 Largest Number

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

  2. 【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 寻找两次最大值 排序 大顶堆 日期 题目地址:htt ...

  3. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  8. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  9. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

随机推荐

  1. 爬取知乎热榜标题和连接 (python,requests,xpath)

    用python爬取知乎的热榜,获取标题和链接. 环境和方法:ubantu16.04.python3.requests.xpath 1.用浏览器打开知乎,并登录 2.获取cookie和User—Agen ...

  2. 20155310 2016-2017-2 《Java程序设计》第九周学习总结

    20155310 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC入门 •数据库本身是个独立运行的应用程序 •撰写应用程序是利用通信协议对数据库进行指 ...

  3. 20155333 2016-2017-2《Java程序设计》课程总结

    20155333 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:你期望的师生关系是什么? 预备作业2:体会做中学(Learing By Doi ...

  4. 考研编程练习----Kruskal

    #include <stdio.h> #include <stdlib.h>   #define MAX 100   /* 定义边(x,y),权为w */ typedef st ...

  5. 从原理到代码:大牛教你如何用 TensorFlow 亲手搭建一套图像识别模块 | AI 研习社

    从原理到代码:大牛教你如何用 TensorFlow 亲手搭建一套图像识别模块 | AI 研习社 PPT链接: https://pan.baidu.com/s/1i5Jrr1N 视频链接: https: ...

  6. 人脸识别引擎SeetaFaceEngine中Identification模块使用的测试代码

    人脸识别引擎SeetaFaceEngine中Identification模块用于比较两幅人脸图像的相似度,以下是测试代码: int test_recognize() { const std::stri ...

  7. Mysql优化分页

    背景: 库里面有张表,日增数据量百万条: 之前查询: SELECT * FROM `res_battery_data_history` LIMIT 1797000,10;

  8. day 9 追踪一个蓝色的物体

    # -*- coding: utf- -*- import cv2 import numpy as np #.打开摄像头 cap=cv2.VideoCapture('output.avi') ): # ...

  9. 【LG2183】[国家集训队]礼物

    [LG2183][国家集训队]礼物 题面 洛谷 题解 插曲:不知道为什么,一看到这个题目,我就想到了这个人... 如果不是有\(exLucas\),这题就是\(sb\)题... 首先,若\(\sum_ ...

  10. Python3中IO文件操作的常见用法

    首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r”   # ...