-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ71.py
More file actions
42 lines (40 loc) · 1.29 KB
/
Q71.py
File metadata and controls
42 lines (40 loc) · 1.29 KB
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
41
42
class Solution:
def simplifyPath(self, path: str) -> str:
#wrong for test case /...
# result = []
# i,n = 1,len(path)
#
# while i<n:
# preC = path[i-1]
# nowC = path[i]
# if preC.isalnum():
# temp = ""
# while path[i-1].isalnum():
# temp+=path[i-1]
# i+=1
# result.append(temp)
# else:
# if preC == "/":
# while i<n+1 and path[i-1] == "/":
# i+=1
# continue
# else:
# if nowC == ".":
# result = result[:-1]
# i+=2
# else:
# i+=1
# result = ["/"+val for val in result]
# if len(result) == 0:
# return "/"
# return "".join(result)
plist = [p for p in path.split('/') if p]
stack = []
for p in plist:
if p == '..':
if stack: stack.pop()
elif p != '.':
stack.append(p)
return '/' + '/'.join(stack)
if __name__ == '__main__':
print(Solution().simplifyPath("/..."))