[leed code 179] Largest Number
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的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- leetcode 179. Largest Number 求最大组合数 ---------- java
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number -- 数字字符串比较大小
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 179. Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
随机推荐
- dbc file
DBC文件是用来描述CAN网络通信信号的一种格式文件.它可以用来监测与分析CAN网络上的报文数据,也可以用来模拟某个CAN节点.(DBC file is a format file used to d ...
- Spring 注解驱动(一)基本使用规则
Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...
- Web应用获取文件路径的方法
拥有 HttpServletRequest req 对象 req.getSession().getServletContext().getRealPath("/") ----- ...
- javase高级技术 - 反射
在说反射之前,必须得先说说java的类加载器,类加载器的定义:将.class文件加载到内在中,并为之生成对应的Class对象. 一般有三种 1 Bootstrap ClassLoader 根类加载器也 ...
- jQuery之JSP加载JS文件不起作用的有效解决方法
JSP加载JS文件不起作用的有效解决方法 作者: 字体:[增加 减小] 类型:转载 时间:2014-04-08 jsp导入jquery文件,老是不起作用,原因在于其不能访问/WEB-INF/目录下的文 ...
- OneZero第三周第四次站立会议(2016.4.7)
1. 时间: 18:35--18:50 共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http: ...
- word表格如何实现序号自动填充
打开word文档,我们需要在如下表格中的准考证号这一列中输入准考证号,手工输入肯定很慢,且容易出错. 我们先选中需要填充准考证号的表格. 选择功能区域中的“开始”,在“段落”组中点击“编号”按 ...
- MyBatis 实现新增
MyBatis实现新增 1.概念学习:(角度不同) 1.1 功能:从应用程序角度出发,软件具有哪些功能 1.2 业务:完成功能时的逻辑,对应Service中一个方法 1.3 事务:从数据库角度出发,完 ...
- Codeforces 1103 简要题解(持续更新)
文章目录 A题 B题 C题 D题 传送门 又一场原地爆炸的比赛. A题 传送门 简单思维题 题意:给一个4∗44*44∗4的格子图和一个01串,你要根据01串放1∗21*21∗2的木块,如果是0就竖放 ...
- mysql的myBatis,主键自增设置
方法一: insert id="insert" parameterType="Person" useGeneratedKeys="true" ...