Leetcode 71. Simplify Path

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Simplify Path

2. Solution

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def simplifyPath(self, path: str) -> str:
result = []
data = path.split('/')
for temp in data:
if temp == '' or temp == '.':
continue
elif temp == '..':
if len(result) != 0:
result.pop()
else:
result.append(temp)

result = '/' + '/'.join(result)
return result

Reference

  1. https://leetcode.com/problems/simplify-path/
如果有收获,可以请我喝杯咖啡!