百度 无人车 面试
作者:互联网
题1
let nums = [30,31,34,5,9] //输出:"9534330" function compare(a, b) { let al = a.length; let bl = b.length; let l = al > bl ? al : bl; for (let i = 0; i < l; i++) {
// 此处重点 比较 3 与 31 的大小, 3要大于 31 let av = a[i] || a[i-1] let bv = b[i] || b[i-1] if(av > bv){ return 1 }else if(av < bv){ return -1 } } return 0 } function quicksort(arr){ if(arr.length <= 1){ return arr } let mid = Math.floor(arr.length/2) let midVal = arr.splice(mid,1)[0] let left = [],right = [] for(let i = 0; i< arr.length; i++){ let iv = arr[i]; console.log(midVal,iv,'ivvvv') if(compare(midVal,iv) > 0){ right.push(iv) }else{ left.push(iv) } } return quicksort(left).concat([midVal]).concat(quicksort(right)) } ret let ret = quicksort(nums.map(item => item + '')) console.log(ret,'ret')
标签:return,quicksort,al,ret,面试,let,av,无人,百度 来源: https://www.cnblogs.com/hill-foryou/p/15150714.html