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个数我们可以找到类似的性质 ...
随机推荐
- 【转】Linux下如何清除系统日志
使用过Windows的人都知道,在使用windows系统的过程中系统会变得越来越慢.而对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然系统将会变得越来越大,越来越迟钝!window ...
- 有关C# struct的一个误区
参考:http://blog.csdn.net/onlyou930/article/details/5568319 下面写一个新的例子: using System; using System.Coll ...
- SQL Server中查询某个数据库中表的个数
--SQL Server中 数表: SELECT * FROM SYSOBJECTS WHERE xtype = 'U'; SELECT COUNT(*) FROM SYSOBJECTS WHERE ...
- t4 加载文件到解决方案
Use EnvDTE add/remove multiple files to project By admin | décembre 17, 2013 Un commentaire Project ...
- Android --差缺补漏之 Intent&putExtra()
伴随着Android Developers 的开发,再也不用FQ了,这意味着Android 对中国学习者有着越来越多的官方学习资料,学习起来有更明确的方向和目标. Android Developer ...
- 伪装的方式实现js继承
看起来属性放到"父类"里了,这种方式原型中还是有属性的,只不过在"子类"的构造器中用call方法调用父类构造函数的时候,"子类"被强行赋值了 ...
- 详细讲解Linux驱动程序
一 编写Linux驱动程序 1.建立Linux驱动骨架 Linux内核在使用驱动时需要装载与卸载驱动 装载驱动:建立设备文件.分配内存地址空间等:module_init 函数处理驱动初始化 卸载驱动 ...
- CCF 201612-2 工资计算 java 解题
问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假设他一个月的税前工资(扣除五险一金后.未扣税前的工资)为S元,则他应交的个人所得税按如下公式计算: 1) 个人所得 ...
- linux清理内存
free -m echo 1 > /proc/sys/vm/drop_caches
- TypeScript 中的 "=>" 真的很好用!!!
class funs { public $scope: IBarPadScope; constructor($scope: IBarPadScope) { this.$scope = $scope; ...