LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
SOLUTION 1:
参考http://bookshadow.com/weblog/2015/01/13/leetcode-largest-number/的解法:
贪心思路:对于两个备选数字a和b,如果str(a) + str(b) > str(b) + str(a),则a在b之前,否则b在a之前
按照此原则对原数组从大到小排序即可
时间复杂度O(nlogn)
易错样例:
Input: [0,0]
Output: "00"
Expected: "0"
JAVA CODE:
public class Solution {
public String largestNumber(int[] num) {
// 1045
// 1111 begin.
if (num == null) {
return null;
}
ArrayList<Integer> list = new ArrayList<Integer>();
for (int n1: num) {
list.add(n1);
}
Collections.sort(list, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2) {
String s1 = "" + o1 + o2;
String s2 = "" + o2 + o1;
return s2.compareTo(s1);
}
});
StringBuilder sb = new StringBuilder();
for (int n: list) {
sb.append(n);
}
if (sb.charAt() == '') {
return "";
}
return sb.toString();
}
}
附上一些关于Comparator ,comparable 的解释:
Comparator 是一个独立的比较器,里面implements "compare",而Comparable 可以由class本身来implement。
http://stackoverflow.com/questions/4108604/java-comparable-vs-comparator
Java : Comparable vs Comparator [duplicate]
When your class implements Comparable, the compareTo method of the class is defining the "natural" ordering of that object. That method is contractually obligated (though not demanded) to be in line with other methods on that object, such as a 0 should always be returned for objects when the .equals() comparisons return true.
A Comparator is its own definition of how to compare two objects, and can be used to compare objects in a way that might not align with the natural ordering.
For example, Strings are generally compared alphabetically. Thus the "a".compareTo("b") would use alphabetical comparisons. If you wanted to compare Strings on length, you would need to write a custom comparator.
In short, there isn't much difference. They are both ends to similar means. In general implement comparable for natural order, (natural order definition is obviously open to interpretation), and write a comparator for other sorting or comparison needs.
相关reference:
http://examples.javacodegeeks.com/core-java/util/comparator/java-comparator-example/
CompareTo示例:
以下的小例子展示了CompareTo的用法:
Card 这个class 使用compareTo函数来实现内建的排序规则,输出如下:
false
1
2
4
public static void main(String[] strs) {
int[] num = {, };
largestNumber(num);
ArrayList<Card> cards = new ArrayList<Card>();
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card();
cards.add(card1);
cards.add(card2);
cards.add(card3);
if (card1.compareTo(card2) > ) {
System.out.println("true");
} else {
System.out.println("false");
}
// uses compareTo method implemented in Card object to compare them
Collections.sort(cards);
// uses compare method implements in Comparator class
//Collections.sort(cards, new CompareBySuitRank());
for (Card card : cards)
System.out.println(card.toString());
}
public static class Card implements Comparable<Card>{
int val;
public String toString() {
return "" + val;
}
public Card(int val) {
super();
this.val = val;
}
public int compareTo(Card o) {
return this.val - o.val;
}
}
Github:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LargestNumber.java
LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...
- 【原创】leetCodeOj --- Largest Number 解题报告
原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, ...
- LeetCode: Valid Number 解题报告
Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【九度OJ】题目1040:Prime Number 解题报告
[九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- 如何使用 Git LFS 提交大文件?
参考资料: An open source Git extension for versioning large files Git LFS的使用 如何使用 Git LFS 提交大文件? Git LFS ...
- GCC&&GDB在OI中的介绍
序言 这本来是用Word写的,但是后来我换了系统所以只能用markdown迁移然后写了...... $\qquad$本文主要投食给那些在Windows下活了很久然后考试时发现需要用命令行来操作时困惑万 ...
- dns 监控系统 设计 dns安全威胁的可视化。
基于DNS大数据分析实现宽带共享监控系统.实现对宽带用户进行有效管理. 本系统基于DNS大数据分析实现宽带共享监控系统,包括以下方面. 1)数据采集:数据采集过程是通过探针采集的方式,从各地市的DNS ...
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- BZOJ.4293.[PA2015]Siano(线段树)
题目链接 \(Description\) 有一片n亩的土地,要在这上面种草. 在每一亩土地上都种植了一种独一无二的草,其中,第\(i\)亩土地的草每天会长高\(a[i]\)厘米. 一共会进行\(m\) ...
- mysql如何查看数据库的存放位置
使用如下命令: mysql> show global variables like "%datadir%";法一: 数据库文件存放在这个位置, C:\ProgramData\ ...
- awk 入门教程
作者: 阮一峰 日期: 2018年11月 7日 awk是处理文本文件的一个应用程序,几乎所有 Linux 系统都自带这个程序. 它依次处理文件的每一行,并读取里面的每一个字段.对于日志.CSV 那 ...
- SpringMVC类型转换、数据绑定详解
public String method(Integer num, Date birth) { ... } Http请求传递的数据都是字符串String类型的,上面这个方法在Controller中定义 ...
- mysql 字符串分割 和 动态执行拼接sql
本人以前主要用的是MSSQL,最近项目在使用MYSQL,自己是一个 典型的小白.今天就记录一下 一个mysql存储过程,里面需要分割字符串和 动态执行sql语句. 关于字符串 分割我开始使用 LOCA ...
- 服务 AIDL 定向tag IPC Parcelable 案例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...