Posted on 2023-12-23 21:51
Uriel 阅读(32)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
给出一列数,包含NSWE代表朝这四个方向走一步,问是否会经过相同的格点,用python的dict记录每次到达的格点即可
1 #1496
2 #Runtime: 16 ms (Beats 59.29%)
3 #Memory: 13.5 MB (Beats 76.11%)
4
5 class Solution(object):
6 def isPathCrossing(self, path):
7 """
8 :type path: str
9 :rtype: bool
10 """
11 x, y = 0, 0
12 cor = {(x, y)}
13 for ch in path:
14 if ch == 'N':
15 y += 1
16 elif ch == 'S':
17 y -= 1
18 elif ch == 'E':
19 x += 1
20 else:
21 x -= 1
22 if (x, y) in cor:
23 return True
24 cor.add((x, y))
25 return False