Nuggets team number online, help you Offer impromptu! Click on theCheck the details
preface
The first time to participate in the Nuggets punch card activity, among other things, mainly rush to reward. 4.12 Start to rush to achieve the small goal of question 14!!
First of all, open LeetCode to find algorithm problems, and come to a problem of solving sort + daily punching
Topic describes
Maximum number
For the title description, I mainly use the screenshot leetcode, so the title is as follows
Thought analysis
And we know from the problem, this is taking an array and then making up a number, and then taking the largest number that’s made up. The first analysis: first of all, use all the numbers so that the number can be the largest, use all the cases have n(the length of the array) factorial possible and then solve this n! Max value of the case. That’s the dumbest way to do it and it works, but I’m not going to try that here
So let’s do a little bit more analysis, let’s make this number as big as possible, so it’s going to be bigger, so I don’t have to make all the combinations, so the first thing I want to do is sort, ${a}${b} ${a}${b} ${a}
I failed the test
If the input value ==0, it will output ‘0’ directly, and then test the wave again
AC code
var largestNumber = function(nums) { if(nums&&nums.length>0){ let arr = nums.sort((a,b)=>{ return (b.toString()+a.toString())-(a.toString()+b.toString()) }) let totle = '' arr.forEach(item=>{ totle+= item.toString() }) if(totle==0){ return '0' }else{ return totle } }else{ return '0' } };Copy the code
conclusion
Be careful, or you’ll have a bug. Today is the first day to brush algorithm, persistence is victory
Left left left
→ Algorithm series link ←
Write write write
You can order it here! You can order it here! You can order it here!