From cfd16f8d54fc8bff95a73a864664f2cc1b2cc083 Mon Sep 17 00:00:00 2001 From: Qiming Xu <458173774@qq.com> Date: Tue, 23 Jan 2024 00:30:50 +0000 Subject: [PATCH] leetcode: finished #752 --- content/leetcode/2024/1.md | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/content/leetcode/2024/1.md b/content/leetcode/2024/1.md index b653e48b..576fad02 100644 --- a/content/leetcode/2024/1.md +++ b/content/leetcode/2024/1.md @@ -3,6 +3,47 @@ title: 2024.1 draft: false --- +# 2024.1.23 + +```python +# +# @lc app=leetcode.cn id=752 lang=python3 +# +# [752] 打开转盘锁 +# + + +# @lc code=start +class Solution: + def openLock(self, deadends: List[str], target: str) -> int: + def neighbors(node): + for i in range(4): + x = int(node[i]) + for d in (-1, 1): + y = (x + d) % 10 + yield node[:i] + str(y) + node[i + 1 :] + + dead = set(deadends) + if "0000" in dead: + return -1 + q = deque([("0000", 0)]) + seen = {"0000"} + + while q: + node, depth = q.popleft() + if node == target: + return depth + for nei in neighbors(node): + if nei not in seen and nei not in dead: + seen.add(nei) + q.append((nei, depth + 1)) + + return -1 + + +# @lc code=end +``` + # 2024.1.22 ```python