-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path71_SimplifyPath.py
executable file
·58 lines (51 loc) · 1.26 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
if not path:
return "/"
stack = []
path_str = ""
index = 0
while index < len(path):
char = path[index]
if char == "/":
# './' respresent current directory
if path_str == "." or path_str == "":
path_str = ""
# '../' represent parent directory
elif path_str == "..":
if stack:
stack.pop()
path_str = ""
# 'path/': push path to stack
else:
stack.append(path_str)
path_str = ""
else:
path_str += char
index += 1
# Append the last path
if path_str == "..":
if stack:
stack.pop()
elif path_str == "." or path_str == "":
pass
else:
stack.append(path_str)
return "/" + "/".join(stack)
"""
""
"/"
"/.."
"/home.as//"
"/home.as"
"/a/./b/../../c/"
"/a/./b/../../c/../"
"/a/./b/c/../.."
"/..."
"""