leetcode71. Simplify Path

Posted seyjs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode71. Simplify Path相关的知识,希望对你有一定的参考价值。

题目如下:

解题思路:首先把路径以\'/\'分割成数组,接下来遍历数组,遇到空或者\'.\'直接删除,遇到\'..\'删除本身和前一个元素。

代码如下:

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]

 

以上是关于leetcode71. Simplify Path的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 71: Simplify Path

LeetCode-71-Simplify Path

leetcode71. Simplify Path

[LeetCode] 71. Simplify Path 简化路径

leetcode 71 Simplify Path ------ java

一天一道LeetCode#71. Simplify Path