->这个符号仅仅是用于帮助阅读代码的,删掉也不影响代码
def add(x, y) -> int:
return x+y
add(1.2,2)
Out[34]: 3.2
所以并没什么用。
再看一个判断机器人路径重叠的例子。
class Robot:
def isPathCrossing(self, path: str) -> bool:
dirs = {
"N": (-1, 0),
"S": (1, 0),
"W": (0, -1),
"E": (0, 1),
}
x, y = 0, 0
coordinates = set([(x, y)])
for ch in path:
dx, dy = dirs[ch]
x, y = x + dx, y + dy
if (x, y) in coordinates:
return True
coordinates.add((x, y))
return False
ps=Robot()
print(ps.isPathCrossing('NWSE'))
True
更多数学原理小文请关注公众号:未名方略