力扣:1598:文件夹操作日志搜集器

This commit is contained in:
huangge1199 2021-04-01 13:20:28 +08:00
parent bf9ea9343d
commit 21ec363b20
2 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,84 @@
//每当用户执行变更文件夹操作时LeetCode 文件系统都会保存一条日志记录
//
// 下面给出对变更操作的说明
//
//
// "../" 移动到当前文件夹的父文件夹如果已经在主文件夹下 继续停留在当前文件夹
// "./" 继续停留在当前文件夹
// "x/" 移动到名为 x 的子文件夹中题目数据 保证总是存在文件夹 x
//
//
// 给你一个字符串列表 logs 其中 logs[i] 是用户在 ith 步执行的操作
//
// 文件系统启动时位于主文件夹然后执行 logs 中的操作
//
// 执行完所有变更文件夹操作后请你找出 返回主文件夹所需的最小步数
//
//
//
// 示例 1
//
//
//
// 输入logs = ["d1/","d2/","../","d21/","./"]
//输出2
//解释执行 "../" 操作变更文件夹 2 即可回到主文件夹
//
//
// 示例 2
//
//
//
// 输入logs = ["d1/","d2/","./","d3/","../","d31/"]
//输出3
//
//
// 示例 3
//
// 输入logs = ["d1/","../","../","../"]
//输出0
//
//
//
//
// 提示
//
//
// 1 <= logs.length <= 103
// 2 <= logs[i].length <= 10
// logs[i] 包含小写英文字母数字'.' '/'
// logs[i] 符合语句中描述的格式
// 文件夹名称由小写英文字母和数字组成
//
// Related Topics
// 👍 12 👎 0
package leetcode.editor.cn;
//1598:文件夹操作日志搜集器
public class CrawlerLogFolder {
public static void main(String[] args) {
//测试代码
Solution solution = new CrawlerLogFolder().new Solution();
solution.minOperations(new String[]{"d1/", "d2/", "../", "d21/", "./"});
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minOperations(String[] logs) {
int step = 0;
int size = logs.length;
for (String str : logs) {
if (str.equals("../")) {
step = step == 0 ? 0 : step - 1;
} else if (!str.equals("./")) {
step++;
}
}
return step;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,53 @@
<p>每当用户执行变更文件夹操作时LeetCode 文件系统都会保存一条日志记录。</p>
<p>下面给出对变更操作的说明:</p>
<ul>
<li><code>&quot;../&quot;</code> :移动到当前文件夹的父文件夹。如果已经在主文件夹下,则 <strong>继续停留在当前文件夹</strong></li>
<li><code>&quot;./&quot;</code> :继续停留在当前文件夹<strong></strong></li>
<li><code>&quot;x/&quot;</code> :移动到名为 <code>x</code> 的子文件夹中。题目数据 <strong>保证总是存在文件夹 <code>x</code></strong></li>
</ul>
<p>给你一个字符串列表 <code>logs</code> ,其中 <code>logs[i]</code> 是用户在 <code>i<sup>th</sup></code> 步执行的操作。</p>
<p>文件系统启动时位于主文件夹,然后执行 <code>logs</code> 中的操作。</p>
<p>执行完所有变更文件夹操作后,请你找出 <strong>返回主文件夹所需的最小步数</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_11_1957.png" style="height: 151px; width: 775px;"></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;../&quot;,&quot;d21/&quot;,&quot;./&quot;]
<strong>输出:</strong>2
<strong>解释:</strong>执行 &quot;../&quot; 操作变更文件夹 2 次,即可回到主文件夹
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_22_1957.png" style="height: 270px; width: 600px;"></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;./&quot;,&quot;d3/&quot;,&quot;../&quot;,&quot;d31/&quot;]
<strong>输出:</strong>3
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>logs = [&quot;d1/&quot;,&quot;../&quot;,&quot;../&quot;,&quot;../&quot;]
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= logs.length &lt;= 10<sup>3</sup></code></li>
<li><code>2 &lt;= logs[i].length &lt;= 10</code></li>
<li><code>logs[i]</code> 包含小写英文字母,数字,<code>&#39;.&#39;</code><code>&#39;/&#39;</code></li>
<li><code>logs[i]</code> 符合语句中描述的格式</li>
<li>文件夹名称由小写英文字母和数字组成</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 12</li><li>👎 0</li></div>