[leetcode] 71. Simplified path


Give an absolute path to a file in UNIX style, you need to simplify it. Or, in other words, convert it to a canonical path.

In UNIX style file systems, a dot (.) represents the current directory itself; in addition, two dots (..) represent switching the directory to the next level (pointing to the parent directory); both can be part of a complex relative path. For more information, see: absolute path vs relative path in Linux / Unix

Note that the returned canonical path must always start with a slash /, and that there must be only one slash /, between two directory names. The last directory name, if present, cannot end with /. In addition, the canonical path must be the shortest string representing the absolute path.


Example 1:

Enter: “/ home /”
Output: ‘/ home’
Explanation: note that there is no slash after the last directory name.
Example 2:

Input: “/.. /”
Output: “/”
Explanation: it’s not feasible to go up from the root, because the root is the highest level you can reach.
Example 3:

Enter: “/ home / / foo /”
Output: “/ home / foo”
Explanation: in the specification path, multiple consecutive slashes need to be replaced with one slash.
Example 4:

Enter: “/ A /. / B /.. / C /”
Output: “/ C”
Example 5:

Enter: “/ A /.. / B /.. / C / /. / /”
Output: “/ C”
Example 6:

Enter: “/ A / / B / / / C / D / /. /. / /..”
Output: “/ A / B / C”

Build stack

Recommended Today

Redis design and implementation 4: Dictionary Dict

In redis, the dictionary is the infrastructure. Redis database data, expiration time and hash type all take the dictionary as the underlying structure. Structure of dictionary Hashtable The implementation code of hash table is as follows:dict.h/dictht The dictionary of redis is implemented in the form of hash table. typedef struct dictht { //Hash table array, […]