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. Unity3d之个性化鼠标

    代码实例: using UnityEngine; using System.Collections; public class CursorController : MonoBehaviour { / ...

  2. 借用Google API在线生成网站二维码地址方法

    二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...

  3. 第3.3 案例2: 工作队列 job queue

    第2个案例就是工作队列,典型的点对点的消息,一个Producer发送一个工作消息到队列去,具有Listener类的Consumer能够从工作队列中获得一个工作情况的消息,这个消息被这个消费者消费掉之后 ...

  4. X3850M2安装CertOS 7 KVM

    在旧的X3850中安装linux系统,研究KVM. 通过选择fedora和centos测试,最终选择centos7来安装. 先安装了一台,安装第二台时又出现第一台的问题,决定记录下来防止记忆出错. 1 ...

  5. Cairo 下载,测试

    You need to download the all-in-one bundle available here. You can discover this link yourself by vi ...

  6. window上利用pip安装pandas

    官网推荐的是直接使用Anoconda,它集成了pandas,可以直接使用.安装挺简单的,有windows下的安装包.如果不想安装庞大的Anoconda,那就一步一步用pip来安装pandas.下面我主 ...

  7. 第九章 硬件抽象层:HAL

    这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...

  8. Unity中游戏的声音管理

    using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 用 ...

  9. 关于解析P D X P 协议的心得

    1一个线程进队Quee 一个线程出队 也应该lock,不然会出错. 2 委托的效率较低 能不用委托的地方,尽量不要用委托. 在一个线程中需要调用控件时采用委托. 3 for循环中异步发送数据不能保证发 ...

  10. 有关attribute和property,以及各自对select中option的影响

    这个问题老生常谈,但是直到现在我依旧时常会把它搞混.下面列一些各自的特性.   attribute property 设置方法 option.setAttribute('selected', true ...