美文网首页
179. Largest Number

179. Largest Number

作者: 7ccc099f4608 | 来源:发表于2020-03-19 13:31 被阅读0次

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

    image.png

    (图片来源https://leetcode-cn.com/problems/largest-number/

    日期 是否一次通过 comment
    2020-03-19 0

    public String largestNumber(int[] num) {
            if(num == null || num.length == 0) {
                return "";
            }
    
            // Convert int array to String array, so we can sort later on
            List<String> sNum = Arrays.stream(num).
                    mapToObj(String::valueOf).
                    sorted((x,y) -> (y+x).compareTo(x+y)).
                    collect(Collectors.toList());
    
    
            if(sNum.get(0).charAt(0) == '0') {
                return "0";
            }
    
            return String.join("", sNum);
        }
    
    
    
    public String largestNumber2(int[] num) {
            if(num == null || num.length == 0)
                return "";
    
            // Convert int array to String array, so we can sort later on
            String[] sNum = new String[num.length];
            for(int i = 0; i < num.length; i++){
                sNum[i] = String.valueOf(num[i]);
            }
    
    
            Arrays.sort(sNum, (x,y) -> (y+x).compareTo(x+y));
            // An extreme edge case by lc, say you have only a bunch of 0 in your int array
            if(sNum[0].charAt(0) == '0'){
                return "0";
            }
            
    
            return String.join("", sNum); //  String.join("", s_num) 太耗时间了
    
        }
    

    相关文章

      网友评论

          本文标题:179. Largest Number

          本文链接:https://www.haomeiwen.com/subject/lptlyhtx.html