好不容易写出来,结果空间超了
#去哪儿旅行秋招# 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前端笔试#
全部评论
相关推荐
11-24 10:46
山东大学 嵌入式软件工程师 给一个offer求了:他也还好吧,跟大部分人比他算是很幸运的了,感觉他三句话离不开北大,但是如果他出生在距离北京八中50公里以外的河北省,他的人生会截然不同诶。京津冀总给人一种特别割裂的感觉。
点赞 评论 收藏
分享
11-23 03:19
University of Miami Java 点赞 评论 收藏
分享