Skip to content

Commit 4eb7673

Browse files
committed
feat: leetcode 1958
1 parent 39038e0 commit 4eb7673

File tree

8 files changed

+141
-1
lines changed

8 files changed

+141
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:488-green" alt="进度:488">
5+
<img src="https://img.shields.io/badge/-进度:489-green" alt="进度:489">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,12 @@
24112411
"path": "./problemset/number-of-ways-to-reconstruct-a-tree/README.md",
24122412
"difficulty": "困难"
24132413
},
2414+
{
2415+
"id": "1958",
2416+
"title": "文件夹操作日志搜集器",
2417+
"path": "./problemset/crawler-log-folder/README.md",
2418+
"difficulty": "简单"
2419+
},
24142420
{
24152421
"id": "1961",
24162422
"title": "检查字符串是否为数组前缀",

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4619,6 +4619,16 @@
46194619
"url": "https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram/",
46204620
"path": "./problemset/check-if-the-sentence-is-pangram/README.md"
46214621
},
4622+
{
4623+
"id": "1958",
4624+
"title": {
4625+
"cn": "文件夹操作日志搜集器",
4626+
"en": "crawler-log-folder"
4627+
},
4628+
"difficulty": "简单",
4629+
"url": "https://leetcode.cn/problems/crawler-log-folder/",
4630+
"path": "./problemset/crawler-log-folder/README.md"
4631+
},
46224632
{
46234633
"id": "1961",
46244634
"title": {

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@
443443
| 1672. [最富有客户的资产总量](../../problemset/richest-customer-wealth/README.md) | 简单 |
444444
| 1706. [球会落何处](../../problemset/where-will-the-ball-fall/README.md) | 中等 |
445445
| 1719. [重构一棵树的方案数](../../problemset/number-of-ways-to-reconstruct-a-tree/README.md) | 困难 |
446+
| 1958. [文件夹操作日志搜集器](../../problemset/crawler-log-folder/README.md) | 简单 |
446447
| 1961. [检查字符串是否为数组前缀](../../problemset/check-if-string-is-a-prefix-of-array/README.md) | 简单 |
447448
| 2016. [增量元素之间的最大差值](../../problemset/maximum-difference-between-increasing-elements/README.md) | 简单 |
448449
| 2028. [找出缺失的观测数据](../../problemset/find-missing-observations/README.md) | 中等 |

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@
924924

925925
[1832. 判断句子是否为全字母句](../../problemset/check-if-the-sentence-is-pangram/README.md)
926926

927+
[1958. 文件夹操作日志搜集器](../../problemset/crawler-log-folder/README.md)
928+
927929
[1961. 检查字符串是否为数组前缀](../../problemset/check-if-string-is-a-prefix-of-array/README.md)
928930

929931
[1984. 学生分数的最小差值](../../problemset/minimum-difference-between-highest-and-lowest-of-k-scores/README.md)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 文件夹操作日志搜集器
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/crawler-log-folder/
6+
7+
## 题目
8+
9+
每当用户执行变更文件夹操作时,`LeetCode` 文件系统都会保存一条日志记录。
10+
11+
下面给出对变更操作的说明:
12+
13+
- `"../"` :移动到当前文件夹的父文件夹。如果已经在主文件夹下,则 继续停留在当前文件夹 。
14+
- `"./"` :继续停留在当前文件夹。
15+
- `"x/"` :移动到名为 `x` 的子文件夹中。题目数据 保证总是存在文件夹 `x`
16+
17+
给你一个字符串列表 `logs` ,其中 `logs[i]` 是用户在 `ith` 步执行的操作。
18+
19+
文件系统启动时位于主文件夹,然后执行 `logs` 中的操作。
20+
21+
执行完所有变更文件夹操作后,请你找出 **返回主文件夹所需的最小步数**
22+
23+
### 示例
24+
25+
#### 示例 1:
26+
27+
![image](https://user-images.githubusercontent.com/54696834/189265713-a7a070bf-d7f5-4d96-8cd6-1a2f8c429681.png)
28+
29+
```
30+
输入:logs = ["d1/","d2/","../","d21/","./"]
31+
输出:2
32+
解释:执行 "../" 操作变更文件夹 2 次,即可回到主文件夹
33+
```
34+
35+
#### 示例 2:
36+
37+
![image](https://user-images.githubusercontent.com/54696834/189265732-a8bf17f3-25a7-4aec-9b89-1d64f69c3b8c.png)
38+
39+
```
40+
输入:logs = ["d1/","d2/","./","d3/","../","d31/"]
41+
输出:3
42+
```
43+
44+
#### 示例 3:
45+
46+
```
47+
输入:logs = ["d1/","../","../","../"]
48+
输出:0
49+
```
50+
51+
## 解题
52+
53+
```ts
54+
/**
55+
* 模拟
56+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
57+
* @param logs
58+
* @returns
59+
*/
60+
export function minOperations(logs: string[]): number {
61+
let depth = 0
62+
for (const log of logs) {
63+
if (log === './') {
64+
continue
65+
}
66+
else if (log === '../') {
67+
if (depth > 0) depth--
68+
}
69+
else {
70+
depth++
71+
}
72+
}
73+
return depth
74+
}
75+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { minOperations } from '.'
3+
4+
describe('文件夹操作日志搜集器', () => {
5+
testCase(minOperations)
6+
})
7+
8+
function testCase(fn: (logs: string[]) => number) {
9+
it.each([
10+
[
11+
['d1/', 'd2/', '../', 'd21/', './'],
12+
2,
13+
],
14+
[
15+
['d1/', 'd2/', './', 'd3/', '../', 'd31/'],
16+
3,
17+
],
18+
[
19+
['d1/', '../', '../', '../'],
20+
0,
21+
],
22+
])('示例%#', (logs, expected) => {
23+
expect(fn(logs)).toBe(expected)
24+
})
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 模拟
3+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
4+
* @param logs
5+
* @returns
6+
*/
7+
export function minOperations(logs: string[]): number {
8+
let depth = 0
9+
for (const log of logs) {
10+
if (log === './') {
11+
continue
12+
}
13+
else if (log === '../') {
14+
if (depth > 0) depth--
15+
}
16+
else {
17+
depth++
18+
}
19+
}
20+
return depth
21+
}

0 commit comments

Comments
 (0)