加载中...
1598-文件夹操作日志搜集器(Crawler Log Folder)
发表于:2021-12-03 | 分类: 简单
字数统计: 1.1k | 阅读时长: 5分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/crawler-log-folder

英文原文

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

  • "../" : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
  • "./" : Remain in the same folder.
  • "x/" : Move to the child folder named x (This folder is guaranteed to always exist).

You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in logs are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

 

Example 1:

Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.

Example 2:

Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3

Example 3:

Input: logs = ["d1/","../","../","../"]
Output: 0

 

Constraints:

  • 1 <= logs.length <= 103
  • 2 <= logs[i].length <= 10
  • logs[i] contains lowercase English letters, digits, '.', and '/'.
  • logs[i] follows the format described in the statement.
  • Folder names consist of lowercase English letters and digits.

中文题目

每当用户执行变更文件夹操作时,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] 符合语句中描述的格式
  • 文件夹名称由小写英文字母和数字组成

通过代码

高赞题解

图解每日一练.jpg


🧠 解题思路

熟读题意,我们可以得知规则如下:

  1. 遇到 ../,返回主文件的层级 -1。
  2. 遇到 ./,返回主文件的层级无变化。
  3. 遇到 x/,返回主文件的层级 +1。

注意:若当前层级 $>=$ 主文件层级时,我们的计数只需要保持为 $0$ 即可。

既然规则如此简单,那我们直接使用计数法就可以搞定了。


🎨 图解演示

<1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg>


🍭 示例代码

[]
var minOperations = function(logs) { let ans = 0; logs.forEach(item => { if(item === '../') ans = Math.max(0, ans - 1); else if(item !== './') ans++; }); return ans; };
[]
class Solution { public: int minOperations(vector<string>& logs) { int ans = 0; for(const string& log: logs){ if(log == "../") ans = max(0, ans - 1); else if(log != "./") ans++; } return ans; } };
[]
class Solution { public int minOperations(String[] logs) { int ans = 0; for (int i = 0; i < logs.length; i++) { if (logs[i].equals("../")) ans = Math.max(0, ans - 1); else if(!logs[i].equals("./")) ans++; } return ans; } }
[]
func minOperations(logs []string) int { result := 0 for _, v := range logs { op := v[:2] switch op { case "..": if result > 0 { result-- } case "./": default: result++ } } return result }
[]
class Solution: def minOperations(self, logs: List[str]) -> int: ans = 0 for log in logs: if log.startswith("../"): ans -= 1 elif log.startswith("./"): ans += 0 else: ans += 1 ans = ans if ans > 0 else 0 return ans

转身挥手

嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐

差点忘了,祝你牛年大吉 🐮 ,AC 和 Offer 📑 多多益善~

⛲⛲⛲ 期待下次再见~

统计信息

通过次数 提交次数 AC比率
11590 17241 67.2%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1595-连通两组点的最小成本(Minimum Cost to Connect Two Groups of Points)
下一篇:
1599-经营摩天轮的最大利润(Maximum Profit of Operating a Centennial Wheel)
本文目录
本文目录