[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');
};

95 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.

  12. I wanted to post you the bit of note just to say thanks a lot once again for the spectacular thoughts you have provided on this site. It was certainly generous of you to allow without restraint just what most of us could have distributed for an e book to make some bucks on their own, chiefly considering the fact that you could have tried it in the event you desired. These solutions likewise acted like the good way to know that other people online have the same dreams just like mine to grasp a whole lot more in respect of this matter. Certainly there are several more pleasant situations in the future for those who go through your website.

  13. I precisely desired to thank you very much all over again. I’m not certain the things I could possibly have carried out in the absence of these solutions discussed by you about this area of interest. It was before a real intimidating setting for me, nevertheless taking a look at a new well-written tactic you managed the issue forced me to jump with gladness. I am just thankful for this information and thus sincerely hope you find out what a powerful job you are putting in teaching most people with the aid of a site. Probably you have never met any of us.

  14. Bertindak beserta slot88 lokasi slot luar praja serta
    untuk menyodorkan ketenteraman daripada layanan tontonan yang berkelanjutan tersuguh 24/7.
    Serta karakteristik ini, Anda dapat bermain bilamana jua dengan pada mana
    melulu minus ada arti keadaan. Bermain slot luar ibu pertiwi bersama tempat web SLOT
    THAILANDmemiliki setengah keuntungan dibandingkan atas dolan slot online dalam negara asal
    Anda. Selain itu, akun pro slot server Thailand terkadang kali mengijabkan layanan pelanggan nan lebih baik selanjutnya lebih responsif dibandingkan dan akun slot biasa.
    Pada bantuan atas status tengara tempat slot gacor enteng menang, sira dapat meregistrasi akun slot online
    terbaik serupa cepat, gampang beserta gratis.
    Dengan akun pro slot kamboja, Anda tak perlu gelisah kembali lantaran letak slot nan mudah
    menang ditawari selama hadirin yang sedia menjumpai jackpot maxwin setiap musim.

    Dalami bersama geledah info bagaimanakah dialek Anda dapat pilih game slot memakai kelas RTP
    tinggi nan hendak mempermudah lalu memungkinkannya Anda demi menetapkan hasil kemajuan gambling slot semakin banyak serta lebih remeh kembali.
    Secara umum pilihan permainan tersebut nan akan bawa tuah lalu nasib nan mana ini hendak mempermudah kau untuk dapat secepatnya kembali bekal.

  15. I read this paragraph fully on the topic of the resemblance of most recent
    and preceding technologies, it’s amazing article.

  16. SLOT GACOR
    Slot Gacor Gampang Menang : Gate Of Olympus , Starlight Princess, Koi Gate , Bonanza
    Provider Slot Online Terbaik : Pragmatic Play , PG Soft
    Promo Unggulan : Event Scatter , Petir Merah , Bonus Begadang 78%
    Pembayaran : BCA , BRI , BNI , Mandiri
    E-Wallet : Dana , Ovo , Gopay , Pulsa
    Minimal Deposit : Rp 25.000,-

  17. SLOT GACOR
    Slot Gacor Gampang Menang : Gate Of Olympus , Starlight Princess,
    Koi Gate , Bonanza
    Provider Slot Online Terbaik : Pragmatic Play , PG Soft
    Promo Unggulan : Event Scatter , Petir Merah ,
    Bonus Begadang 78%
    Pembayaran : BCA , BRI , BNI , Mandiri
    E-Wallet : Dana , Ovo , Gopay , Pulsa
    Minimal Deposit : Rp 25.000,-

  18. Having read this I thought it was very enlightening.
    I appreciate you spending some time and effort to put this information together.
    I once again find myself spending way too much time both reading and commenting.
    But so what, it was still worth it!

  19. SLOT GACOR
    Slot Gacor Gampang Menang : Gate Of Olympus , Starlight Princess, Koi Gate ,
    Bonanza
    Provider Slot Online Terbaik : Pragmatic Play , PG Soft
    Promo Unggulan : Event Scatter , Petir Merah , Bonus Begadang 78%
    Pembayaran : BCA , BRI , BNI , Mandiri
    E-Wallet : Dana , Ovo , Gopay , Pulsa
    Minimal Deposit : Rp 25.000,-

  20. SLOT GACOR
    Slot Gacor Gampang Menang : Gate Of Olympus , Starlight Princess, Koi Gate , Bonanza
    Provider Slot Online Terbaik : Pragmatic Play , PG Soft
    Promo Unggulan : Event Scatter , Petir Merah , Bonus Begadang 78%
    Pembayaran : BCA , BRI , BNI , Mandiri
    E-Wallet : Dana , Ovo , Gopay , Pulsa
    Minimal Deposit : Rp 25.000,-

  21. Hey just wanted to give you a quick heads up.
    The text in your content seem to be running off the screen in Ie.
    I’m not sure if this is a format issue or something to do with internet browser compatibility but
    I thought I’d post to let you know. The design and style look great though!
    Hope you get the problem fixed soon. Kudos

  22. That is the precise blog for anyone who desires to find out about this topic. You realize so much its nearly onerous to argue with you (not that I actually would need匟aHa). You positively put a new spin on a topic thats been written about for years. Nice stuff, just nice!

  23. Bravo pour cet article passionnant ! L’utilisation de l’IA dans les logiciels
    de vente est un sujet qui m’interpelle particulièrement.

  24. It’s actually very difficult in this active life to listen news on TV,
    thus I just use the web for that reason, and obtain the latest information.

  25. My husband and i have been quite delighted when Edward managed to complete his inquiry through your precious recommendations he gained through your site. It is now and again perplexing to simply find yourself giving out things which usually people today could have been trying to sell. We really take into account we need the blog owner to be grateful to for this. All of the explanations you’ve made, the simple web site menu, the friendships you will make it possible to engender – it’s all overwhelming, and it’s really leading our son and our family reckon that this situation is satisfying, and that is rather mandatory. Thank you for all!

  26. I just couldn’t depart your site before suggesting that I extremely loved the standard information a person supply in your visitors?
    Is going to be back steadily in order to check up on new posts

  27. Can I simply say what a relief to uncover somebody that genuinely knows what they
    are discussing over the internet. You actually know how to bring an issue to light and make it important.
    A lot more people should look at this and understand this side of the
    story. I was surprised that you are not more popular given that you surely
    have the gift.

  28. ทางเข้าpg-โพไซดอน เป็นชื่อเรียกเทพเจ้าตามชาวกรีก แต่สำหรับชาวโรมัน ซึ่งรับเอาวัฒนธรรมของกรีกมาอีกทอดหนึ่งจะเรียกว่าเนปจูนตามภาษาละตินเป็นเทพเจ้าแห่งท้องทะเล และมหาสมุทรเป็นผู้ปกครองดินแดน แห่งท้องน้ำ ตั้งแต่แหล่งน้ำจืดเช่น แม่น้ำ ลำคลอง จนถึงใต้บาดาลมีอาวุธคือสามง่ามโพเซดอนเป็นบุตรของโครโนสกับเรมีพี่น้องอีก 4 องค์ ซึ่งล้วนแต่เป็นเทพแห่งโอลิมปัสทั้งสิ้น ได้แก่ ซุส ฮาเดส เฮรา เฮสเตีย ซึ่งความเชื่อสำหรับโพไซดอนนั้นสำหรับคนเล่น สล็อตเว็บตรง คือจะช่วยสาดโชคลาภมาให้แก่คนเล่น สล็อตpg และยังช่วยรางวัลเมกะมาให้อีกด้วยเป็นสิ่งที่เชื่อในหมู่คนเล่น สล็อตเว็บตรง เป็นต้นสามารถเล่นเกมส์ Atlantis Rising ได้ที่ สล็อตpg แล้ววันนี้ Thai named CLUBRED666

  29. I must show some thanks to the writer just for bailing me out of such a condition. Right after browsing throughout the internet and getting principles that were not helpful, I thought my entire life was over. Living without the presence of answers to the issues you have sorted out as a result of your entire report is a critical case, and ones that might have negatively damaged my career if I had not encountered your site. Your main natural talent and kindness in handling a lot of stuff was vital. I don’t know what I would’ve done if I had not come upon such a point like this. I am able to at this time look ahead to my future. Thanks for your time very much for the expert and amazing help. I will not hesitate to recommend the website to any individual who ought to have assistance on this situation.

  30. Comprehending SR22 insurance is vital for drivers that
    require it. SR22 insurance is not an insurance, yet a form verifying insurance protection. This requirement commonly
    observes major traffic offenses. Apply in keeping your
    SR22 insurance to prevent legal concerns.

  31. Pingback: porna

Leave a Reply to kd13 Cancel reply

Your email address will not be published. Required fields are marked *