Leetcode 179: largest number

原创 2016年08月30日 12:35:09

Leetcode 179: largest number

https://leetcode.com/problems/largest-number/

1, what is the largest number looks like?

2, how to make the arrangement in order to make the number large?
example:
1,2,5,34,36 –> 1243436, 5363421

3, the first idea is do the comparation carefully
1) find the largest highest digit in all numbers (may not find the final largest number) : 9123, 89, 77,
2) if there is two same largest digits, compare them and find the second largest digit in the two numbers
3) may use string comparation
String.sort()
Then I come up code like this:

public static String largestNumber(int[] nums) {
    String[] a = new String[nums.length];
    for (int i = 0; i < nums.length; ++i) {
        a[i] = nums[i] + "";
    }
    Arrays.sort(a, Collections.reverseOrder());
    String res = "";
    for (String num : a) {
        res += num;
    }
    return res;
}

4, when I finish the code, I found my solution cannot pass the test case: [121, 12]
1) there two possible arrangement: 12112, 12121
2) if we chose string comparation, the largest number should be 121, and the smaller would be 12
3) that means, if two numbers first few digits are same,
for example: 886,88,
we need to check other numbers in the nums[],
if there is 7x.. we need to chose 887x.. instead of 886..

5, So the custom comparation has some problems:
it need to compare the sum of the different arrangement of the two numbers:
for example: 121 and 12
we need to compare 12112 and 12121
Another exmaple: 98 and 999
we need to compare 98999 and 99998

6, the final code would be a little change:

public String largestNumber(int[] nums) {
    String[] a = new String[nums.length];
    for (int i = 0; i < nums.length; ++i) {
        a[i] = nums[i] + "";
    }
    Arrays.sort(a,(str1,str2) -> (str2 + str1).compareTo(str1 + str2));
    if(a[0].equals("0")) return "0";
    String res = "";
    for (String num : a) {
        res += num;
    }
    return res;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode]179.Largest Number

【题目】 Given a list of non negative integers, arrange them such that they form the largest number...

LeetCode题解(Week2):179. Largest Number

原题目 Given a list of non negative integers, arrange them such that they form the largest number. Fo...

LeetCode(179) Largest Number

题目如下: Given a list of non negative integers, arrange them such that they form the largest number. F...

leetcode_[python/C++] 179. Largest Number(数字组合成最大数)

题目链接 【题目】 Given a list of non negative integers, arrange them such that they form the largest numb...

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 构造最大数字

给出一组非负数字,对它们进行整理排列使它们构成最大的数字。例如,给出[3, 30, 34, 5, 9],可以构成的最大的数字是9534330。

<LeetCode OJ> 179. Largest Number

179. Largest Number My Submissions Question Total Accepted: 37152 Total Submissions: 208760 Diff...

leetcode179. Largest Number

179. Largest NumberGiven a list of non negative integers, arrange them such that they form the large...

LeetCode 179. Largest Number

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

Leetcode.179 Largest Number

Problem description : Given a list of non negative integers, arrange them such that they form the l...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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