generated from zeikar/issueage
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
mediumMediumMedium
Description
Problem link
https://leetcode.com/problems/simplify-path/
Problem Summary
unix path가 주어질 때 간단화 하는 문제.
즉, 여러 슬래시나 .. 같은 디렉토리 변경 등을 다 처리후 최소화된 경로로 만드는 문제.
Solution
스택을 쓰면 되는 간단한 문제.
먼저 슬래시 기준으로 split을 해준다. 일반적인 ., .. 같은 디렉토리 명렁어가 아닌 경우 스택에 넣어주고, ..의 경우에는 스택에서 빼주면 된다. 비어있는 문자열은 연속된 슬래시이므로 무시하면 된다.
마지막으로 슬래시로 시작되어야 하므로 /를 맨 앞에 넣고 리턴하면 된다.
Source Code
class Solution:
def simplifyPath(self, path: str) -> str:
stack = []
split = path.split('/')
for i in range(len(split)):
s = split[i]
if s == '.':
continue
elif s == '..':
if len(stack) >= 1:
stack.pop()
elif s != '':
stack.append(s)
return '/' + '/'.join(stack)Metadata
Metadata
Assignees
Labels
mediumMediumMedium