My Blog List

[Leetcode Solution] Simplify path

Analysis 

  • Use a stack to record the current path hierarchy, each element in the stack correspond to a folder in the path
  • Iterate the input path string and maintain the path stack. parse the sub string between two slashes
    • if sub string is /. or empty, do nothing
    • if sub string is /.. path stack.pop_back 
    • if sub string is a folder name, path stack.push_back

Note

Code


UPDATED AT LEETCODE 2ND PASS

No comments:

Post a Comment

Enter you comment