https://leetcode.com/problems/open-the-lock/description/?envType=daily-question&envId=2024-04-22
functionopenLock(deadends,target){letdeadSet=newSet(deadends);letqueue=["0000"];letvisited=newSet(["0000",...deadends]);letturns=0;while(queue.length>0){letsize=queue.length;for(leti=0;i<size;i++){letcurrent=queue.shift();if(current===target)returnturns;if(deadSet.has(current))continue;for(letj=0;j<4;j++){letdigit=parseInt(current[j]);letnext1=current.substring(0,j)+(digit===9?0:digit+1)+current.substring(j+1);letnext2=current.substring(0,j)+(digit===0?9:digit-1)+current.substring(j+1);if(!visited.has(next1)||!visited.has(next2)){queue.push(next1);visited.add(next1);queue.push(next2);visited.add(next2);}}}turns++;}return-1;}
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse