here is a simple python code for this question, but I can't figure out a test case
class Solution: # @param num, a list of integers # @return a string def largestNumber(self, num): num = [str(x) for x in num] num.sort(cmp=lambda x, y: cmp(y+x, x+y)) ret = ''.join(num) return ret.lstrip('0') or '0'
The idea is simple, first I convert all int in num to strings, then provide a custom compare function. In this lambda function, just concatenate and compare (y+x) and (x+y, the reason to switch (x+y) and (y+x) is because we want this array to be sorted in decreasing order.
cmp(22, 221) -> should form 22221 (because 22221>22122), hence cmp(22,221) == -1
Most of the test cases run through fine but the longest test case
Failed saying "Wrong Answer" and there is no Output.
I tried this test case on my own machine and the function works perfectly.
I guess there is something wrong with the system for python?
The fix was just pushed live. Please try again, you should get Accepted. sorry for the inconvenience.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.