题目如下:
解题思路:首先把路径以'/'分割成数组,接下来遍历数组,遇到空或者'.'直接删除,遇到'..'删除本身和前一个元素。
代码如下:
class Solution(object): def simplifyPath(self, path): """ :type path: str :rtype: str """ l = path.split('/') inx = 0 #print l while inx < len(l) and inx >= 0: if l[inx] == '' or l[inx] == '.': del l[inx] elif l[inx] == '..': del l[inx] if inx - 1 >= 0: del l[inx-1] inx -= 1 else: inx += 1 res = '/' for i in l: res += i res += '/' if len(res) == 1: return res return res[:-1]