|
7 | 7 |
|
8 | 8 | ## 71. Simplify Path (Medium)
|
9 | 9 |
|
10 | | -<p>Given an absolute path for a file (Unix-style), simplify it. </p> |
| 10 | +<p>Given an <strong>absolute path</strong> for a file (Unix-style), simplify it. Or in other words, convert it to the <strong>canonical path</strong>.</p> |
11 | 11 |
|
12 | | -<p>For example,<br /> |
13 | | -<strong>path</strong> = <code>"/home/"</code>, => <code>"/home"</code><br /> |
14 | | -<strong>path</strong> = <code>"/a/./b/../../c/"</code>, => <code>"/c"</code><br /> |
15 | | -<strong>path</strong> = <code>"/a/../../b/../c//.//"</code>, => <code>"/c"</code><br /> |
16 | | -<strong>path</strong> = <code>"/a//b////c/d//././/.."</code>, => <code>"/a/b/c"</code></p> |
| 12 | +<p>In a UNIX-style file system, a period <code>.</code> refers to the current directory. Furthermore, a double period <code>..</code> moves the directory up a level. For more information, see: <a href="https://www.linuxnix.com/abslute-path-vs-relative-path-in-linuxunix/" target="_blank">Absolute path vs relative path in Linux/Unix</a></p> |
17 | 13 |
|
18 | | -<p>In a UNIX-style file system, a period ('.') refers to the current directory, so it can be ignored in a simplified path. Additionally, a double period ("..") moves up a directory, so it cancels out whatever the last directory was. For more information, look here: <ahref="https://en.wikipedia.org/wiki/Path_(computing)#Unix_style">https://en.wikipedia.org/wiki/Path_(computing)#Unix_style</a></p> |
| 14 | +<p>Note that the returned canonical path must always begin with a slash <code>/</code>, and there must be only a single slash <code>/</code> between two directory names. The last directory name (if it exists) <b>must not</b> end with a trailing <code>/</code>. Also, the canonical path must be the <strong>shortest</strong> string representing the absolute path.</p> |
19 | 15 |
|
20 | | -<p><strong>Corner Cases:</strong></p> |
| 16 | +<p> </p> |
21 | 17 |
|
22 | | -<ul> |
23 | | - <li>Did you consider the case where <strong>path</strong> = <code>"/../"</code>?<br /> |
24 | | - In this case, you should return <code>"/"</code>.</li> |
25 | | - <li>Another corner case is the path might contain multiple slashes <code>'/'</code> together, such as <code>"/home//foo/"</code>.<br /> |
26 | | - In this case, you should ignore redundant slashes and return <code>"/home/foo"</code>.</li> |
27 | | -</ul> |
| 18 | +<p><strong>Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input: "</strong><span id="example-input-1-1">/home/"</span> |
| 22 | +<strong>Output: "</strong><span id="example-output-1">/home" |
| 23 | +<strong>Explanation:</strong> Note that there is no trailing slash after the last directory name.</span> |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong>Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input: "</strong><span id="example-input-1-1">/../"</span> |
| 30 | +<strong>Output: "</strong><span id="example-output-1">/"</span> |
| 31 | +<strong>Explanation:</strong> Going one level up from the root directory is a no-op, as the root level is the highest level you can go. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong>Example 3:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input: "</strong><span id="example-input-1-1">/home//foo/"</span> |
| 38 | +<strong>Output: "</strong><span id="example-output-1">/home/foo"</span> |
| 39 | +<strong>Explanation: </strong>In the canonical path, multiple consecutive slashes are replaced by a single one. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong>Example 4:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input: "</strong><span id="example-input-1-1">/a/./b/../../c/"</span> |
| 46 | +<strong>Output: "</strong><span id="example-output-1">/c"</span> |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p><strong>Example 5:</strong></p> |
| 50 | + |
| 51 | +<pre> |
| 52 | +<strong>Input: "</strong><span id="example-input-1-1">/a/../../b/../c//.//"</span> |
| 53 | +<strong>Output: "</strong><span id="example-output-1">/c"</span> |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p><strong>Example 6:</strong></p> |
| 57 | + |
| 58 | +<pre> |
| 59 | +<strong>Input: "</strong><span id="example-input-1-1">/a//b////c/d//././/.."</span> |
| 60 | +<strong>Output: "</strong><span id="example-output-1">/a/b/c"</span> |
| 61 | +</pre> |
28 | 62 |
|
29 | 63 |
|
30 | 64 | ### Related Topics
|
|
0 commit comments