71. Simplify Path absolute path to compressed file

[copy title]:

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

 [brute force solution]:

Time analysis:

Space Analysis:

 [Optimized]:

Time analysis:

Space Analysis:

[Wonderful output conditions]:

[Wonderful corner case]:

Corner Cases:

    • Did you consider the case where path = "/../"?
      In this case, you should return "/".
    • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
      In this case, you should ignore redundant slashes and return "/home/foo".

[Thinking question]:

[One sentence idea]:

[Input amount]: Empty: Normal situation: Extra large: Extra small: Special situations handled in the program: Abnormal situations (Illegal and unreasonable input):

[Paint]:

[One brush]:

[Second brush]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

  [Results of five-minute naked eye debug]:

[Summarize]:

[Complexity]: Time complexity: O( ) Space complexity: O( )

[English data structures or algorithms, why not use other data structures or algorithms]:

Go from left to right:

"/b/c/" - directory 'b ' - > directory 'c '
"." - current directory
"./" - current directory
"../" - one directory up
eg
"/" : root directory
"b/c/../" : it will go from c to b
"c/b/./" : it is still in directory b

[Algorithm idea: recursion/divide and conquer/greedy]:

[Key templating code]:

[Other solutions]:

[Follow Up]:

[The topics given by LC change and change]:

 [Code style]:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897541&siteId=291194637