阅读背景:

linux路径简化

来源:互联网 
class Solution:
    def simplifyPath(self, path: str) -> str:
        result = ''
        tmp = ''
        for i in path:
            if i == '.':
                if tmp == '/' or tmp == '/.':
                    tmp += i
                else:
                    tmp = ''
            elif i == '/':
                if tmp == '/.':
                    result = result[:-2]
                    tmp = i
                elif tmp == '/..':
                    result = result[:-3]
                    while result and result[-1] != '/':
                        result = result[:-1]
                    else:
                        if not result:
                            result = '/'
                    tmp = i
                    continue
                elif tmp == '/':
                    continue
                else:
                    tmp = i
            else:
                tmp = ''
            result += i
        if result[-1] == '.':
            if result[-2] == '.':
                if result[-3] == '/':
                    result = result[:-3]
                    while result and result[-1] != '/':
                        result = result[:-1]
                    else:
                        if not result:
                            result = '/'
            elif result[-2] == '/':
                result = result[:-1]
        if len(result) > 1 and result[-1] == '/':
            result = result[:-1]
        return result
class Solution:
    def simplifyPath(self, p



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: