diff --git a/leetcode/1501-1600/1598.Crawler-Log-Folder/README.md b/leetcode/1501-1600/1598.Crawler-Log-Folder/README.md index 991c31dc1..290d32908 100755 --- a/leetcode/1501-1600/1598.Crawler-Log-Folder/README.md +++ b/leetcode/1501-1600/1598.Crawler-Log-Folder/README.md @@ -1,28 +1,45 @@ # [1598.Crawler Log Folder][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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:** +**Example 1:** + +![1](./sample_11_1957.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: logs = ["d1/","d2/","../","d21/","./"] +Output: 2 +Explanation: Use this change folder operation "../" 2 times and go back to the main folder. ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./sample_22_1957.png) -### 思路1 -> ... -Crawler Log Folder -```go ``` +Input: logs = ["d1/","d2/","./","d3/","../","d31/"] +Output: 3 +``` + +**Example 3:** +``` +Input: logs = ["d1/","../","../","../"] +Output: 0 +``` ## 结语 diff --git a/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution.go b/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution.go index d115ccf5e..2a0f82d01 100644 --- a/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution.go +++ b/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(logs []string) int { + deep := 0 + for _, op := range logs { + if op == "./" { + continue + } + if op == "../" { + deep = max(0, deep-1) + continue + } + deep++ + } + return deep } diff --git a/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution_test.go b/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution_test.go index 14ff50eb4..4e582509d 100644 --- a/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution_test.go +++ b/leetcode/1501-1600/1598.Crawler-Log-Folder/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"d1/", "d2/", "../", "d21/", "./"}, 2}, + {"TestCase2", []string{"d1/", "d2/", "./", "d3/", "../", "d31/"}, 3}, + {"TestCase3", []string{"d1/", "../", "../", "../"}, 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_11_1957.png b/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_11_1957.png new file mode 100644 index 000000000..c428cc71b Binary files /dev/null and b/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_11_1957.png differ diff --git a/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_22_1957.png b/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_22_1957.png new file mode 100644 index 000000000..2d7ef62e3 Binary files /dev/null and b/leetcode/1501-1600/1598.Crawler-Log-Folder/sample_22_1957.png differ