[Programming Problem] Largest Number

Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.
 
Since the result may be very large, so you need to return a string instead of an integer.
 
Example 1:
Input: nums = [10,2]
Output: "210"
 
Example 2:
Input: nums = [3,30,34,5,9]
Output: "9534330"

Take two numbers, say 3 and 30. How would you decide which one would come first? You would simply compare ‘330’ and ‘303’ and decide.

Given the above comparison function, we simply sort these numbers using a custom comparator which compares two numbers in the above way. Then, just concat array and return number.

One gotcha: the system expects you to remove leading zeros. We use a regex for that.

/**
 * @param {number[]} nums
 * @return {string}
 */
var largestNumber = function(nums) {
    nums.sort((a, b) => Number(a+""+b) > Number(b+""+a) ? -1 : 1);
    return nums.join("").replace(/^0*(.+)/, '$1');
};

12 thoughts on “[Programming Problem] Largest Number

  1. I simply desired to appreciate you once again. I’m not certain the things I could possibly have gone through without these ways discussed by you concerning that industry. It previously was an absolute alarming concern in my opinion, but seeing a new expert way you handled the issue forced me to leap for gladness. I am just happy for your advice as well as sincerely hope you know what an amazing job that you’re providing training the rest thru your websites. Most probably you haven’t got to know any of us.

  2. I must express my admiration for your kind-heartedness supporting people who absolutely need help on this important concern. Your real commitment to passing the message up and down had been exceptionally practical and have in every case encouraged professionals just like me to realize their dreams. Your amazing informative guideline entails this much to me and substantially more to my office colleagues. Best wishes; from everyone of us.

  3. I would like to voice my respect for your generosity in support of those individuals that should have guidance on in this situation. Your personal dedication to passing the solution up and down appeared to be astonishingly effective and have surely helped professionals like me to arrive at their dreams. Your amazing informative key points denotes a great deal to me and a whole lot more to my office colleagues. Thanks a lot; from all of us.

  4. I precisely desired to say thanks yet again. I do not know the things that I could possibly have followed in the absence of the type of thoughts documented by you on my topic. It previously was a horrifying problem for me, however , observing your well-written form you treated the issue forced me to jump for delight. I’m thankful for your service as well as hope you find out what an amazing job you have been getting into training the rest through your website. Most likely you have never come across any of us.

  5. I simply needed to say thanks once more. I am not sure the things that I would’ve gone through in the absence of these secrets revealed by you directly on my subject. It actually was a very alarming condition in my opinion, however , spending time with a expert tactic you processed that took me to weep over contentment. I am just happy for your information and then hope you are aware of a great job you are always providing educating most people by way of a web site. Most probably you have never come across all of us.

  6. I intended to post you the little word to finally give many thanks again for your personal magnificent secrets you’ve contributed on this page. This has been so strangely open-handed with you to offer easily all that a few people might have supplied as an e book to get some money for themselves, notably seeing that you could have tried it in the event you considered necessary. The techniques in addition served to become easy way to be certain that other individuals have the same fervor similar to mine to find out a good deal more with regard to this problem. I know there are several more pleasurable sessions up front for people who go through your site.

  7. I wish to point out my love for your generosity for persons who need help on the question. Your personal commitment to passing the message all around appeared to be rather powerful and have constantly helped some individuals just like me to reach their objectives. The warm and helpful key points denotes much to me and far more to my fellow workers. Many thanks; from all of us.

  8. I precisely had to say thanks once more. I am not sure what I might have followed without those creative concepts documented by you concerning such theme. It was before a real frustrating concern for me personally, however , being able to see your expert mode you solved that made me to cry with happiness. Now i am happy for your information and then trust you recognize what a great job that you’re getting into instructing many people through your website. More than likely you’ve never got to know all of us.

  9. I want to get across my love for your kind-heartedness in support of people who really need help with in this area. Your special dedication to getting the solution all over had been quite helpful and has always permitted others much like me to attain their goals. Your amazing warm and friendly guide means so much to me and especially to my mates. With thanks; from everyone of us.

  10. I wish to point out my appreciation for your kindness in support of individuals that really need guidance on the question. Your special dedication to passing the solution along had been incredibly good and have frequently permitted workers much like me to arrive at their goals. Your insightful hints and tips entails a lot to me and especially to my mates. Warm regards; from each one of us.

  11. I want to voice my respect for your kind-heartedness for folks that really want assistance with your content. Your personal dedication to passing the solution up and down had been exceedingly practical and has encouraged regular people much like me to realize their pursuits. Your amazing important guide entails much to me and even further to my fellow workers. With thanks; from all of us.

Leave a Reply

Your email address will not be published.