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的更多相关文章

  1. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  2. [LeetCode] 179. Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. Example ...

  3. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  5. JavaScript中sort方法的一个坑(leetcode 179. Largest Number)

    在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...

  6. [LeetCode] 179. Largest Number 解题思路

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  7. [leetcode]179. Largest Number最大数

    Given a list of non negative integers, arrange them such that they form the largest number. Input: [ ...

  8. LeetCode 179 Largest Number 把数组排成最大的数

    Given a list of non negative integers, arrange them such that they form the largest number.For examp ...

  9. Leetcode 179 Largest Number 贪心

    此题主要是讲给你一组数,如何将数连在一起能得到最大值(最小值反之),注意局部最优,就是说如果 123 234两个连在一起怎么样最大,显然是234123比123234大,对于3个数我们可以找到类似的性质 ...

随机推荐

  1. myeclipse激活法,可以试一试

    我的myeclipse2014也是这样激活: 第一步:输入任意用户名 第二步:点击Systemid... 按钮,自动生成本机器的systemid. 第三步: 点菜单Tools->RebuildK ...

  2. android 加载中、无网络、无数据、出错 四种状态的代码封装

    package com.weavey.loading.lib;import android.content.Context;import android.content.res.TypedArray; ...

  3. ASP.NET后台调用前台JS函数的三种常见方法

    第一种:使用普通的添加控件中的Attributes属性进行调用 例如,像一般的普通的按钮:Button1.Attributes.Add("onclick","MyFun( ...

  4. 转:js中this、call、apply的用法

    (一)关于this首先关于this我想说一句话,这句话记住了this的用法你也就差不多都能明白了:this指的是当前函数的对象.这句话可能比较绕,我会举出很多例子和这句话呼应的!(看下文)1.首先看下 ...

  5. MySQL 通过semi join 优化子查询

    半连接是MySQL 5.6.5引入的,多在子查询exists中使用,对外部row source的每个键值,查找到内部row source匹配的第一个键值后就返回,如果找到就不用再查找内部row sou ...

  6. JAVA学习心得

    学习JAVA一个学期了,虽然学的不是很好,也没有彻底理解JAVA的真正内涵,但是我认为学习java应该是循环渐进,有始有终,按部就班,脚踏实地.java是一门有着阶梯性的一门语言,信心,恒心,毅力是最 ...

  7. AngulaJs+Web Api Cors 跨域访问失败的解决办法

    //在服务的WebConfig文件中添加以下代码即可 //如节点已存在请去掉 <system.webServer> <httpProtocol> <customHeade ...

  8. 学习SVG系列(3):SVG Stroke属性

    SVG stroke 属性 1.stroke 2.stroke-width 3.stroke-linecap 4.stroke-dasharray 5.stroke-opacity 6.stroke- ...

  9. 修改Android系统属性SystemProperties.set("sys.powerctl", "shutdown")关机分析

    简介: 从之前的博文中我们提到过,关机流程中最后是通过修改Android属性进行关机操作(SystemProperties.java通过JNI调用访问系统属性),当然我们也可以通过adb命令修改And ...

  10. Reverse-Daily(3)-DotNetCrackMe1

    链接:http://pan.baidu.com/s/1cuYQhK    密码:zjx6 这是一个用c#编写的程序 用dotpeek或者ILSPY反编译可以看到程序结构,主体比较简单,是一个des加密 ...