leetcode 179. Largest Number 求最大组合数 ---------- java
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.
给一组数,求这一组数的最大组合。
刚开始想用直接排序的方法:1、最高位较大的放在前面
2、但是就出现了54与5这种情况,那么进一步判断。
3、然后又出现了121与12的情况,就越写越复杂。也没有写对。
public class Solution {
public String largestNumber(int[] nums) {
System.out.println(compare(121,12));
StringBuffer str = new StringBuffer();
sort(nums, 0, nums.length - 1);
for (int i = nums.length - 1;i >= 0; i--){
System.out.print(nums[i]+" ");
str.append(nums[i]);
}
return str.toString();
}
private void sort(int[] nums, int left, int right){
if (left >= right){
return ;
}
int start = left;
int end = right;
int flag = nums[left];
while (left < right){
while (right > left && compare(flag, nums[right]) == -1){
right--;
}
if (left == right){
break;
} else {
nums[left] = nums[right];
nums[right] = flag;
left++;
}
while (right > left && compare(nums[left],flag) == -1){
left++;
}
if (left == right){
break;
} else {
nums[right] = nums[left];
nums[left] = flag;
right--;
} }
for( int i = 0;i < nums.length; i++)
System.out.print(nums[i]+" ");
System.out.println();
sort(nums, start, left - 1);
sort(nums, right + 1, end);
}
private int compare(int num1, int num2){
double num1_copy = num1;
double num2_copy = num2;
while (num1_copy >= 10){
num1_copy = num1_copy / 10;
}
while (num2_copy >= 10){
num2_copy = num2_copy / 10;
}
if ((int) num1_copy % 10 > (int) num2_copy % 10){
return 1;
} else if ((int) num1_copy % 10 < (int) num2_copy % 10){
return -1;
} else {
int flag = (int) num1_copy % 10;
while ((int) num1_copy % 10 == (int) num2_copy % 10 && (int) num1_copy != 0 && (int) num2_copy != 0){
flag = (int) num1_copy % 10;
num1_copy = num1_copy * 10 - ((int) num1_copy % 10) * 10;
num2_copy = num2_copy * 10 - ((int) num2_copy % 10) * 10;
}
System.out.println(num1+" "+num2+" "+num1_copy+" "+num2_copy);
if ((int) num1_copy == 0 ){
if (num2_copy % 10 > flag){
return -1;
}else {
return 1;
}
} else if (num2_copy == (double) 0){
if (num1_copy % 10 > flag){
return 1;
} else {
return -1;
}
}else if (num1_copy % 10 > num2_copy % 10){
return 1;
} else {
return -1;
}
} }
}
2、用另一种方法排序:
直接用String存储数字,两个数字(str1,str2)的大小:
s1 = str1 + str2;
s2 = str2 + str2;
s1.compareTo(s2);
这样比较。
public class Solution {
public String largestNumber(int[] nums) {
String[] strs = new String[nums.length];
for (int i = 0; i < nums.length; i++){
strs[i] = String.valueOf(nums[i]);
}
Comparator<String> comp = new Comparator<String>(){
public int compare(String str1, String str2){
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
};
Arrays.sort(strs, comp);
if (strs[0].charAt(0) == '0'){
return "0";
}
StringBuffer sb = new StringBuffer();
for (String str : strs){
sb.append(str);
}
return sb.toString();
}
}
leetcode 179. Largest Number 求最大组合数 ---------- java的更多相关文章
- 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 for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Java 特定规则排序-LeetCode 179 Largest Number
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码位点)来排,如果要根据大小排序,需要传入一个 ...
- [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最大数
Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...
- LeetCode 179 Largest Number 把数组排成最大的数
Given a list of non negative integers, arrange them such that they form the largest number.For examp ...
- Leetcode 179 Largest Number 贪心
此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...
随机推荐
- AAS代码第2章
[root@node1 aas]# pwd /root/aas [root@node1 aas]# wget http://archive.apache.org/dist/spark/spark-1. ...
- install vim-powerline
1, install pip dnf install python-pip 2,install powerline-status pip install git+git://github.com/Lo ...
- gnome3.X添加开机启动项
背景:升级gnome后发现gnome-session-properties不见了,想把sslocal随机启动遇到了麻烦... 特别说明:此为图形桌面开机启动项,因此只有通过图形桌面登陆用户后才能启动. ...
- 开启GZIP(转)
因为在做一个项目,项目里面服务器主要提供数据,但是数据多了文件就大了,比较浪费流量和时间,我们便用Gzip来处理.我在本机上是apache,服务器上是IIS6.0,用的是php,那么我就在这里分享一下 ...
- web前端基础篇⑩
1.960栅格式布局法屏幕分辨率为1024*768.采用接 main宽为960px的布局方式12列式:每格60px 间距20px 3 6 3版 三格式布局(最常用)16列式:每格40px 间距20px ...
- 关于mybatis一对多关联时
一对多关联时注:collection标签,property属性名称,column参数字段, ofType查询返回类型,select查询方法,javaType方法的返回类型
- 关于docker容器是怎样建立新的namespace的。
最近博客收到了一封交流的私信,感谢您的关注:现在就我理解的docker建立容器时namespace的建立问题做一个 个人的回答: 一,从原理角度来讲: docker创建container,说白了就是l ...
- 重载运算符:类成员函数or友元函数
类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...
- OD18
介绍一个工具exescope 可以修改一些exe程序里的东西 通过这个工具 我们找到了我们要除掉的NAG窗口的具体位置 那我们可以通过OD进行跟踪 来到程序头下段 ...
- 对上次“对字符串进行简单的字符数字统计 探索java中的List功能 ”程序,面向对象的改进
之前的随笔中的程序在思考后发现,运用了太多的static 函数,没有将面向对象的思想融入,于是做出了一下修改: import java.util.ArrayList; import java.util ...