Created
February 17, 2020 05:13
-
-
Save RP-3/136a6e3118953b7ed0e479843bcd52b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {string} | |
*/ | |
var largestNumber = function(nums) { | |
const result = nums | |
.map((num) => num.toString()) | |
.sort((a, b) => { | |
const aBeforeB = parseInt(`${a}${b}`, 10); | |
const bBeforeA = parseInt(`${b}${a}`, 10); | |
return aBeforeB > bBeforeA ? -1 : 1; | |
}) | |
.join(''); | |
return containsOnlyZeroes(result) ? '0' : result.replace(/^0+/, ''); | |
}; | |
function containsOnlyZeroes(str){ | |
for(let i=0; i<str.length; i++) if(str[i] !== '0') return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment