欧科云链前端笔试 10.22
15单项 10不定项 3编程 1问答。难度整体偏简单。
问答题不给跳出编译真的难绷。。。
1. 最长重复子串长度 100%
输入:'ababc' 输出:4 'abab'重复
要求 时间复杂度n^2
思路:感觉跟力扣459 差不多
function solve( a ) { // write code here let max = 0 for(let i=0;i<Math.floor(a.length/2);i++){ for(let j=1;j<Math.floor(a.length/2)+1;j++){ let n= a.substring(i,j) if(a.includes(n.repeat(2))){ max = n.length>max?n.length:max } } } return max*2 }
2. 有重复项的全排列 10%
输入:[1,1,2] 输出:[[1,1,2],[,1,2,1],[2,1,1]]
思路:回溯+剪枝
刷过这道题 但是做的时候时间不够了。。。好可惜啊 回溯写出来了 就差个剪枝
function permuteUnique( num ) { let len = num.length let res = [] let path = [] function tracer(used){ if(path.length===len){ res.push(path.slice()); return; } for(let i=0;i<len;i++){ if(i>0&&num[i]==num[i-1]&&!num[i-1]){ continue } if(!used[i]){ used[i]=true path.push(num[i]) tracer(used) path.pop() used[i]=false } } } tracer([]) return res }
3. %s替换成字符 100%
输入:"A%sC%sE",['B','D','F'] 输出:"ABCDEF"
%s替换成数组内的元素,多的元素放字符串后
保证数组元素个数>%s的数量
思路:把%s替换成% 再遍历字符串跟数组 最后把多的数组加进去字符串后面。属于是暴力解了
function formatString( str , arg ) { // write code here let s = [...str.replace(/\%s/g,'%')] let n = 0 for(let i=0;i<s.length;i++){ if(s[i]=='%'){ s[i]=arg[n++] } } for(let i=n;i<arg.length;i++){ s.push(arg[i]) } return s.join("") }#欧科云链##笔试##欧科云链笔试#