Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件夹操作日志搜集器 #158

Open
yankewei opened this issue Sep 10, 2022 · 1 comment
Open

文件夹操作日志搜集器 #158

yankewei opened this issue Sep 10, 2022 · 1 comment
Labels
字符串 题目类型为字符串 数组 题目类型为数组 题目包含栈解法 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

每当用户执行变更文件夹操作时,LeetCode 文件系统都会保存一条日志记录。

下面给出对变更操作的说明:

  • "../" :移动到当前文件夹的父文件夹。如果已经在主文件夹下,则 继续停留在当前文件夹 。
  • "./" :继续停留在当前文件夹。
  • "x/" :移动到名为 x 的子文件夹中。题目数据 保证总是存在文件夹 x 。

给你一个字符串列表logs,其中logs[i]是用户在第i步执行的操作。

文件系统启动时位于主文件夹,然后执行logs中的操作。

执行完所有变更文件夹操作后,请你找出 返回主文件夹所需的最小步数 。

示例1

image

输入:logs = ["d1/","d2/","../","d21/","./"]
输出:2
解释:执行 "../" 操作变更文件夹 2 次,即可回到主文件夹

示例 2:

image

输入:logs = ["d1/","d2/","./","d3/","../","d31/"]
输出:3

示例 3:

输入:logs = ["d1/","../","../","../"]
输出:0

提示:

  • 1 <= logs.length <= 103
  • 2 <= logs[i].length <= 10
  • logs[i] 包含小写英文字母,数字,'.' 和 '/'
  • logs[i] 符合语句中描述的格式
  • 文件夹名称由小写英文字母和数字组成

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/crawler-log-folder
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added 简单 题目难度为简单 数组 题目类型为数组 字符串 题目类型为字符串 题目包含栈解法 labels Sep 10, 2022
@yankewei
Copy link
Owner Author

yankewei commented Sep 10, 2022

简单的模拟

class Solution {

    /**
     * @param String[] $logs
     * @return Integer
     */
    function minOperations($logs) {
        $result = 0;
        foreach ($logs as $log) {
            if ($log === '../') {
                $result  = $result > 0 ? $result - 1 : $result;
                continue;
            } else if ($log === './') {
                continue;
            } else {
                $result += 1;
            }
        }
        return $result;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
字符串 题目类型为字符串 数组 题目类型为数组 题目包含栈解法 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant