-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path71_SimplifyPath.py
40 lines (37 loc) · 1.04 KB
/
71_SimplifyPath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# https://leetcode.com/problems/simplify-path/
# 2022 3.15
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
# # v1 using str.split
# a=[]
# folders=path.split('/')
# print(folders)
# for i in range(len(folders)):
# if folders[i]=='.' or folders[i]=='' :
# continue
# elif folders[i]=='..':
# if a :
# a.pop()
# else:
# continue
# else:
# a.append(folders[i])
# a.insert(0,'')
# a.append('')
# ret='/'.join(a);
# if len(ret)<2:
# return '/'
# else:
# return ret[:-1]
# v1.1 copied more clean
stack=[]
for item in path.split('/'):
if stack and item=='..':
stack.pop()
elif item not in ['','.','..']:
stack.append(item)
return '/'+'/'.join(stack)