好不容易写出来,结果空间超了
#去哪儿旅行秋招# function isPalindrome(str){
let reser = str.split('').reverse().join('')
return reser === str
}
function find(arrs,n,m){
let myMap = new Map()
for(let i = 0;i < m;i++){
myMap.set(arrs[i],i+1)
}
let res = [];
function backtracking(index,curstr,used){
if(index === arrs.length){
if(isPalindrome(curstr)){
res.push({pal:curstr})
}
return;
}
for(let i= 0;i if(used[i]) continue;
let newstr = curstr + arrs[i];
let newused = [...used]
newused[i] = true;
backtracking(index+1,newstr,newused)
if(newstr.length < 2 * m && index < arrs.length - 1){
let mid = Math.floor(newstr.length/2);
let temp = newstr.slice(0,mid)+arrs[i]+newstr.slice(mid);
backtracking(index+1,temp,newstr)
}
}
}
let initused = new Array(arrs.length).fill(false);
backtracking(0,'',initused)
let orderanswer = [];
for(let i = 0;i < res[0].pal.length;i += m){
let nstr = res[0].pal.substring(i,i+m);
let or = myMap.get(nstr)
orderanswer.push(or)
}
// console.log(orderanswer)
return orderanswer.join(' ')
let result = find(arr,n,m)
console.log(result)
#去哪儿网25前端笔试#
let reser = str.split('').reverse().join('')
return reser === str
}
function find(arrs,n,m){
let myMap = new Map()
for(let i = 0;i < m;i++){
myMap.set(arrs[i],i+1)
}
let res = [];
function backtracking(index,curstr,used){
if(index === arrs.length){
if(isPalindrome(curstr)){
res.push({pal:curstr})
}
return;
}
for(let i= 0;i
let newstr = curstr + arrs[i];
let newused = [...used]
newused[i] = true;
backtracking(index+1,newstr,newused)
if(newstr.length < 2 * m && index < arrs.length - 1){
let mid = Math.floor(newstr.length/2);
let temp = newstr.slice(0,mid)+arrs[i]+newstr.slice(mid);
backtracking(index+1,temp,newstr)
}
}
}
let initused = new Array(arrs.length).fill(false);
backtracking(0,'',initused)
let orderanswer = [];
for(let i = 0;i < res[0].pal.length;i += m){
let nstr = res[0].pal.substring(i,i+m);
let or = myMap.get(nstr)
orderanswer.push(or)
}
// console.log(orderanswer)
return orderanswer.join(' ')
let result = find(arr,n,m)
console.log(result)
#去哪儿网25前端笔试#
全部评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享