`

LeetCode - Largest Number

 
阅读更多

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.

 

各种corner case都要考虑,比如

  • 343和34,谁放到前面?34334<34343,所以34放在前面。
  • 344和34,谁放到前面?34434>34344,所以344放在前面。
  • 所有数都为零的情况,要把前面的零去掉,只留下一个。
  • 可以考虑用PriorityQueue对数据进行排序,或者直接创建数组排序。

 

Solution:

public String largestNumber(int[] num) {
    if(num==null || num.length==0) return "0";
    Queue<String> q = new PriorityQueue<String>(num.length, new Comparator<String>() {
        public int compare(String a, String b) {
            String s1 = a+b;
            String s2 = b+a;
            return s2.compareTo(s1);
        }
    });
    for(int e:num) {
        q.offer(""+e);
    }
    StringBuilder sb = new StringBuilder();
    while(!q.isEmpty()) {
        sb.append(q.poll());
    }
    while(sb.charAt(0)=='0' && sb.length()>1) {
        sb.deleteCharAt(0);
    }
    return sb.toString();
}

 

分享到:
评论

相关推荐

    扩展矩阵leetcode-Leetcode:LeetcodeAnswer-Java

    扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...34.在排序数组中查找元素的第一个和最后一个...largestNumber 324.摆

    LeetCode最全代码

    191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...

    leetcode316-LeetCode:leetcode的解决方案

    Largest Rectangle in Histogram:单调栈 Island Perimeter:简单对比(map+zip的使用) or 遍历查找 Max Area of Island:DFS(本来想用DP,发现出不来) Number of Islands:DFS My Calendar II:小空间匹配 My ...

    leetcode寻找最近的-qyhc:群英荟萃,各种资源,欢迎各位大佬共享笔记或资源

    leetcode寻找最近的 设计模式 ...随时随记 随记笔记 ...最大数(golang)](Alg/leetcode/largestNumber.go [190. 颠倒二进制位(golang)](Alg/leetcode/reverseBits.go Docker 面试题 GO面试题 Redis面试题

    leetcode答案-easy_Maximum-Subarray:easy_Maximum-Subarray

    leetcode 答案 easy_Maximum-Subarray 提交链接 / Submit (You need register/login first before submit.) (在提交前你需要先注册或登录) 题目描述 / Description Given an integer array nums, find the ...

    leetcode装最多水-Leetcode:解决了leetcode问题

    largest element from an unsorted array in linear time. (1) If the number of elements in an array is large .Divide the array into arrays each with 5 elements. n/5 arrays. (2) Compute Median of these 5 ...

    lrucacheleetcode-LeetCodeSheet:记录自己Leetcode之旅

    Number Leetcode 75. Sort Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked ...

    LeetCode C++全解

    1. Introduction ... Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element

    gasstationleetcode-LeetCode:Leet和其他OJ问题

    加油站 leetcode 力码 该文件夹不仅包含算法和数据库的leetcode问题,还包含其他公开...largest distance from the sum of any subarray to the average of subarray (sum(a[n])/(k+1)). Output: a number of the Im

    cpp-算法精粹

    Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II Search a 2D Matrix Search a 2D Matrix II Find Minimum in Rotated ...

    Coding Interview In Java

    leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...

Global site tag (gtag.js) - Google Analytics