Skip to content
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

Open
Sunny-117 opened this issue Sep 23, 2023 · 5 comments
Open

百度2023秋招:把数组排成最小的数 #501

Sunny-117 opened this issue Sep 23, 2023 · 5 comments

Comments

@Sunny-117
Copy link
Owner

image

@JsweetA
Copy link

JsweetA commented Oct 22, 2023

请问该题原题力扣有吗

@realllllty
Copy link

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;

@Sunny-117
Copy link
Owner Author

请问该题原题力扣有吗

@fencesitter1
Copy link

@topulikeweb
Copy link

/**
 * 比如[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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants