LeetCode 179. Largest Number

原创 2016年11月08日 11:42:18

欢迎移步到我的个人博客

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

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.

题意解析

给出一个非负整型列表,对列表里的整型进行排序,使得排序后将这些数字连在一起的数字最大。

例如,非负整型列表为[3, 30, 34, 5, 9],排序后最大的值为9534330

注意:结果可能是一个非常大的值,所以用string代替integer

解法分析

这道题的排序方式为假设有两个数AB,如果AB>BA则排序为AB,否则排序为BA
java中有一个接口为Comparable,如下。

public interface Comparable<T> {
    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     * ...
     */
    public int compareTo(T o);

新建一个类只要实现了这个接口中的compareTo函数,就可以使用Arrays.sort进行排序。

解题代码

    public String largestNumber(int[] nums) {
        List<IntegerShadow> integerShadows = new ArrayList<IntegerShadow>();
        for (int num : nums) {
            if (num == 0) {
                integerShadows.add(new IntegerShadow(0, "0"));
                continue;
            }
            integerShadows.add(new IntegerShadow(num, String.valueOf(num)));
        }
        IntegerShadow[] integerShadows2 = new IntegerShadow[integerShadows
                .size()];
        integerShadows.toArray(integerShadows2);
        Arrays.sort(integerShadows2);
        StringBuilder sb = new StringBuilder();
        for (int i = integerShadows2.length - 1; i >= 0; i--) {
            sb.append(integerShadows2[i].ori);
        }
        if (sb.toString().matches("0+")) {
            return "0";
        } else {
            return sb.toString();
        }
    }

    class IntegerShadow implements Comparable<IntegerShadow> {
        public int ori;
        public String oriStr;

        public IntegerShadow(int o, String s) {
            this.ori = o;
            this.oriStr = s;
        }

        @Override
        public int compareTo(IntegerShadow o) {
            return (this.oriStr + o.oriStr).compareTo(o.oriStr + this.oriStr);
        }
    }

LeetCode(179) Largest Number

题目如下: Given a list of non negative integers, arrange them such that they form the largest number. F...
  • feliciafay
  • feliciafay
  • 2015-01-21 15:58:05
  • 2663

leetcode 179: Largest Number

Largest Number Total Accepted: 2269 Total Submissions: 15366 Given a list of non negative integer...
  • xudli
  • xudli
  • 2015-01-16 04:12:19
  • 4275

Leetcode 179. Largest Number (Medium) (java)

Leetcode 179. Largest Number (Medium) (java)
  • Niko_Ke
  • Niko_Ke
  • 2016-09-22 13:58:37
  • 269

[Leetcode] 179. Largest Number @python

题目Given a list of non negative integers, arrange them such that they form the largest number.For exa...
  • qian2729
  • qian2729
  • 2016-02-05 17:00:05
  • 511

179 Largest Number

题目链接:https://leetcode.com/problems/largest-number/题目:Given a list of non negative integers, arrange ...
  • ChilseaSai
  • ChilseaSai
  • 2016-01-31 22:03:04
  • 793

[leetcode-179]Largest Number(java)

问题描述: Given a list of non negative integers, arrange them such that they form the largest number.Fo...
  • zdavb
  • zdavb
  • 2015-08-29 00:32:39
  • 751

leetcode179-Largest Number(把数组排成最大的数)

问题描述:输入一个正整数数组,把数组里的所有数字拼接起来排成一个数,打印能拼接出的所有数字中最大的一个。例如输入数组{3,32,321},则打印出332321.问题求解:这道题和剑指offer面试题3...
  • will130
  • will130
  • 2016-06-11 11:52:22
  • 1725

【LeetCode】Largest Number 解题报告

【题目】 Given a list of non negative integers, arrange them such that they form the largest number...
  • ljiabin
  • ljiabin
  • 2015-01-13 15:04:39
  • 11401

LeetCode Largest Number 贪心

将数组中的元素进行重排,组成最大的数。 如果只有两个数,可以简单判断:,使用贪心,将数组中的元素按照上述比较规则进行降序排序,进行拼接即为所求。证明如下: 假设按照贪心得到的最优解为:(其中),若存在...
  • ww32zz
  • ww32zz
  • 2016-02-10 22:03:41
  • 288

LeetCode--Largest Number(最大数字)Python

题目: 给定一个包含非负数的列表,重新排列,使其最终组合之后的整数最大。例如:给定列表[3, 30, 34, 5, 9], 返回的最大整数是9534330 解题思路: 相当于对列表中的数字进行排...
  • xiaoxiaoley
  • xiaoxiaoley
  • 2017-11-29 15:13:09
  • 81
收藏助手
不良信息举报
您举报文章:LeetCode 179. Largest Number
举报原因:
原因补充:

(最多只允许输入30个字)