71-最小路径和

71、简化路径

https://leetcode-cn.com/problems/simplify-path/

题目:

a5mPGn.png

题解:

以’/‘为标记分割字符串,双//会出现空,.和..分别代表当前路径和上层路径,分开处理即可。

1
2
3
4
5
6
7
8
9
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
for i in path.split('/'):
if i not in ['', '.', '..']:
stack.append(i)
elif i == '..' and stack:
stack.pop()
return '/' + '/'.join(stack)

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器