-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimplify_Path
36 lines (35 loc) · 996 Bytes
/
Simplify_Path
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
# author: marissazhou
# date: 30/08/2014
# idea: look through all strings between '/' and check sign
class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
if path is None:
return
if len(path)<2:
return path
stack = []
i = 1
while i < len(path):
sstr = ''
while i<len(path) and path[i] is not "/":
sstr = sstr + path[i]
i = i+1
if sstr == "..":
if len(stack) > 0:
stack.pop()
elif sstr == ".":
continue
elif len(sstr)>0:
stack.append(sstr)
if i<len(path) and path[i] == '/':
i = i+1
continue
# reunion
if len(stack) == 0:
return '/'
result = ''
for i in xrange(len(stack)):
result = result+"/"+stack[i]
return result