-
-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
百度2023秋招:把数组排成最小的数 #501
Comments
请问该题原题力扣有吗 |
let snums = new Array(nums.length);
for (let i = 0; i < nums.length; i++) {
snums[i] = String(nums[i]);
}
snums.sort((a, b) => {
let ab = a + b;
let ba = b + a;
return ab - ba;
});
let res = snums.join("");
return res; |
有 |
/**
* 比如[3,30]这个数组,有两种排列[30,3],[3,30],将每种情况进行比较,就可以得出最小
* @param arr
* @returns {*}
*/
function sortArrToStr (arr) {
return arr.sort((a, b) => {
const a_str = a + ''
const b_str = b + ''
return (a_str + b_str) - (b_str + a_str)
}).join('')
}
console.log(sortArrToStr([3, 30]))
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: