//1.单词映射匹配检测 function testMatch( pattern , str ) { pattern = pattern.split(''); pattern; str = str.split(' '); str; if(!pattern.length && !str.length) return true; if(pattern.length !== str.length) return false; var setPattern = Array.from(new Set(pattern)); var setStr = Array.from(new Set(str)); for(let i=0;i<pattern.length;i++){ if(setStr[setPattern.indexOf(pattern[i])]!==str[i]) return false; } return true; } var pattern = "effe",str="good bad bad bad"; console.log(testMatch(pattern,str)); //给定正整数N,调整顺序获得一个比N大的最小正整数 function findGreaterNum(n) { if(n<=10) return n; var list = String(n).split(''); list; for(var i=list.length-1;i>0;i--){ for(var j=i-1;j>0;j--){ if(list[i]>list[j]){ var temp = list[j]; list[j] = list[i]; list[i] = temp; return list.join(''); } } return -1; } } var n =1234; console.log(findGreaterNum(n)); //给定正整数数组A和正整数T,在A中找出两个连续子数组,两个子数组不能相交且两个子数组的和为T,返回方案中两个子数组长度和的最小值。 function minSumOfLengths( A , T ) { var list = []; for(var i=0;i<A.length;i++){ var sum = T; list[i]=[]; for(var j=i;j<A.length;j++){ sum-=A[j]; list[i].push(A[j]); if(sum<0){ list.pop(); break; } if(sum===0) break; } } var newlist=[]; for(var k=0;k<list.length;k++){ if(list[k]) newlist.push(list[k]); } if(newlist.length<2) return -1; newlist = newlist.sort((a,b)=>a.length-b.length); return newlist[0].length+newlist[1].length; } var a1=[3,2,2,4,3],t1=3; var a2=[7,3,4,7],t2=7; var a3=[5,5,4,4,5],t3=3; console.log(minSumOfLengths(a3,t1)); //[3,2,2,4,3],3 ==> 2 //[7,3,4,7],7 ==> 2 //[5,5,4,4,5],3 ==>-1
全部评论
(1) 回帖