Skip to content

Commit 05fc2d0

Browse files
committed
Add solution and test-cases for problem 1598
1 parent 9f83439 commit 05fc2d0

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

leetcode/1501-1600/1598.Crawler-Log-Folder/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [1598.Crawler Log Folder][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The Leetcode file system keeps a log each time some user performs a change folder operation.
5+
6+
The operations are described below:
7+
8+
- `"../"` : Move to the parent folder of the current folder. (If you are already in the main folder, **remain in the same folder**).
9+
- `"./"` : Remain in the same folder.
10+
- `"x/"` : Move to the child folder named x (This folder is **guaranteed to always exist**).
11+
12+
You are given a list of strings `logs` where `logs[i]` is the operation performed by the user at the i<sup>th</sup> step.
13+
14+
The file system starts in the main folder, then the operations in `logs` are performed.
15+
16+
Return the minimum number of operations needed to go back to the main folder after the change folder operations.
717

8-
**Example 1:**
18+
**Example 1:**
19+
20+
![1](./sample_11_1957.png)
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
Input: logs = ["d1/","d2/","../","d21/","./"]
24+
Output: 2
25+
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.
1326
```
1427

15-
## 题意
16-
> ...
28+
**Example 2:**
1729

18-
## 题解
30+
![2](./sample_22_1957.png)
1931

20-
### 思路1
21-
> ...
22-
Crawler Log Folder
23-
```go
2432
```
33+
Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
34+
Output: 3
35+
```
36+
37+
**Example 3:**
2538

39+
```
40+
Input: logs = ["d1/","../","../","../"]
41+
Output: 0
42+
```
2643

2744
## 结语
2845

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(logs []string) int {
4+
deep := 0
5+
for _, op := range logs {
6+
if op == "./" {
7+
continue
8+
}
9+
if op == "../" {
10+
deep = max(0, deep-1)
11+
continue
12+
}
13+
deep++
14+
}
15+
return deep
516
}

leetcode/1501-1600/1598.Crawler-Log-Folder/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"d1/", "d2/", "../", "d21/", "./"}, 2},
17+
{"TestCase2", []string{"d1/", "d2/", "./", "d3/", "../", "d31/"}, 3},
18+
{"TestCase3", []string{"d1/", "../", "../", "../"}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}
Loading
Loading

0 commit comments

Comments
 (0)