码迷,mamicode.com
首页 > 其他好文 > 详细

179. Largest Number

时间:2019-10-01 09:13:20      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:end   you   https   leetcode   and   until   put   lang   list   

import java.lang.StringBuilder

/**
 * 179. Largest Number
 * https://leetcode.com/problems/largest-number/description/
 * https://www.cnblogs.com/grandyang/p/4225047.html
 *
 * Given a list of non negative integers, arrange them such that they form the largest number.

Example 1:
Input: [10,2]
Output: "210"

Example 2:
Input: [3,30,34,5,9]
Output: "9534330"

Note: The result may be very large, so you need to return a string instead of an integer.
 * */
class Solution {
    fun largestNumber(nums: IntArray): String {
        val nums_ = nums.sortedWith(Comparator { a, b ->
            val aString = a.toString()
            val bString = b.toString()
            when {
                (aString + bString) < (bString + aString) -> 1
                (aString + bString) > (bString + aString) -> -1
                else -> 0
            }
        })
        val result = StringBuilder()
        for (i in 0 until nums_.size) {
            if (nums_.get(0)==0){
                return "0"
            } else {
                result.append(nums_.get(i))
            }
        }
        //print(result.toString())
        return result.toString()
    }
}

 

179. Largest Number

标签:end   you   https   leetcode   and   until   put   lang   list   

原文地址:https://www.cnblogs.com/johnnyzhao/p/11614685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!