LeetCode/Top Interview 150

71. Simplify Path

hyunkookim 2024. 12. 6. 16:26

71. Simplify Path

 

내 코드

 

class Solution:
    def simplifyPath(self, path: str) -> str:
        path_list = path.split('/')
        print(path_list)

        stack = []
        for w in path_list:
            if w == '' or w == '.':
                continue
            elif w == '..':
                if len(stack): 
                    stack.pop()
                continue
            stack.append(w)

        print(stack)
        res = ''
        for s in stack:
            res += '/' + s

        return res if res != '' else '/'
class Solution:
    def simplifyPath(self, path: str) -> str:
        path_list = path.split('/')
        #print(path_list)

        stack = []
        for w in path_list:
            if w == '' or w == '.':
                continue
            elif w == '..':
                if len(stack): 
                    stack.pop()
                continue
            stack.append(w)

        #print(stack)        
        return "/" + "/".join(stack)

 

https://youtu.be/qYlHrAKJfyA?si=etRzYbsrxLWY5bHc

 

class Solution:
    def simplifyPath(self, path: str) -> str:
        stack = []
        cur = ""

        for c in path + "/":
            if c == "/":
                if cur == "..":
                    if stack: 
                        stack.pop()
                elif cur != "" and cur != ".":
                    stack.append(cur)
                cur = ""
            else:
                cur += c

        return "/" + "/".join(stack)

'LeetCode > Top Interview 150' 카테고리의 다른 글

224. Basic Calculator  (0) 2024.12.06
150. Evaluate Reverse Polish Notation  (0) 2024.12.06
57. Insert Interval  (0) 2024.12.03
128. Longest Consecutive Sequence  (0) 2024.12.03
155. Min Stack  (0) 2024.12.02