//1.十六进制颜色转RGB格式 function colorRgb(color){ var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; color = color.toLowerCase(); if(reg.test(color)){ if(color.length === 4){ var colorNew = "#"; for(let i=1;i<4;i++){ colorNew += color.slice(i,i+1).concat(color.slice(i,i+1)); } color = colorNew; } var colorChange = []; for(let i=1;i<7;i+=2){ colorChange.push(parseInt("0x"+color.slice(i,i+2))); } return "RGB("+colorChange.join(",")+")"; }else{ return color; } } color = '#1234567'; console.log(colorRgb(color)); //2.找数组中存在子路径的元素 function subPath(arr){ var len = arr.length; var res = []; for(let i=0;i<len;i++){ for(let j=0;j<len;j++){ arr[j]; if(i!==j && !arr[j].indexOf(arr[i]+'/')){ res.push(arr[i]); } } } res = Array.from(new Set(res)); return res; } var arr = ['/a/b','/a/c','/b/c','/b/c/d/e','/b/ef']; console.log(subPath(arr)); //3.走迷宫 const map = '0,0,0,0;1,1,1,0;1,1,1,0'.split(';'); var m=map.length,n=0; for(let i=0;i<map.length;i++){ map[i] = '1,'+map[i]+',1'; map[i] = map[i].split(','); n=map[i].length-2; } var head = new Array(n+2).fill('1'); map.unshift(head); map.push(head); function dfs(map,steps,x,y){ var current_step = steps+1; map[x][y] = current_step; var next_step = current_step+1; if(!(x-1==1 && y==1) && map[x-1][y]!='1' && (map[x-1][y]>next_step || map[x-1][y]=='0')){ //left dfs(map,current_step,x-1,y) } if(!(x==1 && y-1==1) && map[x][y-1]!='1' && (map[x][y-1]>next_step || map[x][y-1]=='0')){ //top dfs(map,current_step,x,y-1) } if(!(x==1 && y+1==1) && map[x][y+1]!='1' && (map[x][y+1]>next_step || map[x][y+1]=='0')){ //bottom dfs(map,current_step,x,y+1) } if(!(x+1==1 && y==1) && map[x+1][y]!='1' && (map[x+1][y]>next_step || map[x+1][y]=='0')){ //right dfs(map,current_step,x+1,y) } } dfs(map,-1,1,1); console.log(map[m][n]) if(map[m][n]>1) console.log('true'); else console.log('false');
全部评论
(0) 回帖