在leetcode上刷题的时遇到一位大佬,写出了远超同行的耗时68ms的代码,看到对数组最短字符串的处理方式,一时叹为观止,遂记录一二,话不多说,上代码

strs = ['123','1234','12345']
let lens = strs.map(item => item.length);
let minLen = Math.min.apply(null, lens);
let str = strs[lens.indexOf(minLen)];

由此方法的启发,我们也可以用以下方法快速得到数组中的最小值

let arr = [3,12,23,18,25,33,22,30,1]
let min = Math.min(...arr); //扩展运算符
console.log(min) //1

 

相关文章:

  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案