Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.43k stars 3.29k forks source link

取一半对称然后比较大小,数字较大时开销比较小 #591

Closed yuewanxinghui closed 1 month ago

yuewanxinghui commented 1 month ago

const fn = (num) => { const res = [] let i = 1 const temp = Math.ceil(num.toString().split('').length / 2) let maxNum = Number(1${[...Array(temp).keys()].map(i => '0').join('')}) const reverseFn = (n) => n.toString().split('').reverse().join('') const tempArr = [...Array(10).keys()] while (i < maxNum) { if (i < 10) { res.push(Number(${i}${i})) } else { const reNum = Number(${i}${reverseFn(i)}) if (reNum < num) { res.push(reNum) } } for(let j = 0; j < 10; j++) { const reNum = Number(${i}${j}${reverseFn(i)}) if (reNum < num) { res.push(reNum) } } i++ } return res }