mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-11 10:21:43 +08:00
批量更新数据
This commit is contained in:
@@ -1,42 +1,85 @@
|
||||
<p>Given a string <code>path</code>, which is an <strong>absolute path</strong> (starting with a slash <code>'/'</code>) to a file or directory in a Unix-style file system, convert it to the simplified <strong>canonical path</strong>.</p>
|
||||
<p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>'/'</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p>
|
||||
|
||||
<p>In a Unix-style file system, a period <code>'.'</code> refers to the current directory, a double period <code>'..'</code> refers to the directory up a level, and any multiple consecutive slashes (i.e. <code>'//'</code>) are treated as a single slash <code>'/'</code>. For this problem, any other format of periods such as <code>'...'</code> are treated as file/directory names.</p>
|
||||
|
||||
<p>The <strong>canonical path</strong> should have the following format:</p>
|
||||
<p>The <em>rules</em> of a Unix-style file system are as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path starts with a single slash <code>'/'</code>.</li>
|
||||
<li>Any two directories are separated by a single slash <code>'/'</code>.</li>
|
||||
<li>The path does not end with a trailing <code>'/'</code>.</li>
|
||||
<li>The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period <code>'.'</code> or double period <code>'..'</code>)</li>
|
||||
<li>A single period <code>'.'</code> represents the current directory.</li>
|
||||
<li>A double period <code>'..'</code> represents the previous/parent directory.</li>
|
||||
<li>Multiple consecutive slashes such as <code>'//'</code> and <code>'///'</code> are treated as a single slash <code>'/'</code>.</li>
|
||||
<li>Any sequence of periods that does <strong>not match</strong> the rules above should be treated as a <strong>valid directory or</strong> <strong>file </strong><strong>name</strong>. For example, <code>'...' </code>and <code>'....'</code> are valid directory or file names.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the simplified <strong>canonical path</strong></em>.</p>
|
||||
<p>The simplified canonical path should follow these <em>rules</em>:</p>
|
||||
|
||||
<ul>
|
||||
<li>The path must start with a single slash <code>'/'</code>.</li>
|
||||
<li>Directories within the path must be separated by exactly one slash <code>'/'</code>.</li>
|
||||
<li>The path must not end with a slash <code>'/'</code>, unless it is the root directory.</li>
|
||||
<li>The path must not have any single or double periods (<code>'.'</code> and <code>'..'</code>) used to denote current or parent directories.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>simplified canonical path</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> path = "/home/"
|
||||
<strong>Output:</strong> "/home"
|
||||
<strong>Explanation:</strong> Note that there is no trailing slash after the last directory name.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">path = "/home/"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"/home"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The trailing slash should be removed.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> path = "/../"
|
||||
<strong>Output:</strong> "/"
|
||||
<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.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">path = "/home//foo/"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"/home/foo"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Multiple consecutive slashes are replaced by a single one.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> path = "/home//foo/"
|
||||
<strong>Output:</strong> "/home/foo"
|
||||
<strong>Explanation:</strong> In the canonical path, multiple consecutive slashes are replaced by a single one.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">path = "/home/user/Documents/../Pictures"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"/home/user/Pictures"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>A double period <code>".."</code> refers to the directory up a level (the parent directory).</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">path = "/../"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"/"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Going one level up from the root directory is not possible.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 5:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">path = "/.../a/../b/c/../d/./"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"/.../b/d"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><code>"..."</code> is a valid name for a directory in this problem.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
Reference in New Issue
Block a user