Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
leetcode: finished #752
Browse files Browse the repository at this point in the history
  • Loading branch information
xqm32 committed Jan 23, 2024
1 parent 82cd6e4 commit cfd16f8
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions content/leetcode/2024/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cfd16f8

Please sign in to comment.