Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
2k stars 236 forks source link

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

Open Sunny-117 opened 1 year ago

Sunny-117 commented 1 year ago

image

JsweetA commented 11 months ago

请问该题原题力扣有吗

realllllty commented 10 months ago
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 commented 8 months ago

请问该题原题力扣有吗

fencesitter1 commented 8 months ago

leetcode上是不是这题 [LCR 164. 破解闯关密码](https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/)

topulikeweb commented 6 months ago
/**
 * 比如[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]))