9 lines of Python code


  • 24
    H
    class Solution(object):
        def simplifyPath(self, path):
            places = [p for p in path.split("/") if p!="." and p!=""]
            stack = []
            for p in places:
                if p == "..":
                    if len(stack) > 0:
                        stack.pop()
                else:
                    stack.append(p)
            return "/" + "/".join(stack)

  • -2
    P

    you use str built-in function like join and split, which is not appropriate in the interview case...


  • 1
    H

    It's appropriate. I always used them in my previous interviews, if possible.


  • 3
    G

    said in 9 lines of Python code:

    if len(stack) > 0:

    more pythonic would be if stack:


  • 8

    You solution is good. I just change a little to make it a little neater.

    def simplifyPath(self, path):
        stack = []
        for p in path.split("/"):
          if p == "..":
            if stack: stack.pop()
          elif p and p != '.': stack.append(p)
        return "/" + "/".join(stack)

  • 0

    No need to consider cases like '...'?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.