179. Largest Number

179- Largest Number
**Question
Editorial Solution

My Submissions

Total Accepted: 50334
Total Submissions: 253493
Difficulty: Medium

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.
Credits:Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags
Sort

  /*The idea here is basically implement a String comparator to decide which String should come first during concatenation. Because when you have 2 numbers (let's convert them into String), you'll face only 2 cases:
        For example:
        
        String s1 = "9";
        String s2 = "31";
        
        String case1 =  s1 + s2; // 931
        String case2 = s2 + s1; // 319
        
        Apparently, case1 is greater than case2 in terms of value.
        So, we should always put s1 in front of s2.
        https://discuss.leetcode.com/topic/8018/my-java-solution-to-share
    */
    public String largestNumberJava7(int[] nums) {
        if (nums == null || nums.length == 0) return null;
        String[] strArr = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            strArr[i] = String.valueOf(nums[i]);
        }
        // Comparator to decide which string should come first in concatenation
        Comparator<String> comp = new Comparator<String>(){
             @Override 
             public int compare(String str1, String str2) {
                 String s1 = str1 + str2;
                 String s2 = str2 + str1;
                 return s2.compareTo(s1); // reverse order here, so we can do append() later
             }
        };
        
        Arrays.sort(strArr, comp);
        
        // An extreme edge case by lc, say you have only a bunch of 0 in your int array
        if(strArr[0].charAt(0) == '0')
           return "0";
            
        
        StringBuilder sb = new StringBuilder();
        for (String str : strArr) {
            sb.append(str);
        }
        
        return sb.toString();
    }
    
    // https://discuss.leetcode.com/topic/7235/my-3-lines-code-in-java-and-python
    public String largestNumber(int[] num) {  // Thanks for Java 8, it makes code beautiful.
        String[] array = Arrays.stream(num).mapToObj(String::valueOf).toArray(String[]::new);
        Arrays.sort(array, (String s1, String s2) -> (s2 + s1).compareTo(s1 + s2));
        return Arrays.stream(array).reduce((x, y) -> x.equals("0") ? y : x + y).get();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容