File tree Expand file tree Collapse file tree 6 files changed +171
-2
lines changed
solution/1500-1599/1598.Crawler Log Folder Expand file tree Collapse file tree 6 files changed +171
-2
lines changed Original file line number Diff line number Diff line change 63
63
64
64
<!-- 这里可写通用的实现逻辑 -->
65
65
66
+ ** 方法一:模拟**
67
+
68
+ 直接模拟,记录深度的变化即可。
69
+
70
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 ` logs ` 的长度。
71
+
66
72
<!-- tabs:start -->
67
73
68
74
### ** Python3**
69
75
70
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
77
72
78
``` python
73
-
79
+ class Solution :
80
+ def minOperations (self , logs : List[str ]) -> int :
81
+ ans = 0
82
+ for v in logs:
83
+ if v == " ../" :
84
+ ans = max (0 , ans - 1 )
85
+ elif v[0 ] != " ." :
86
+ ans += 1
87
+ return ans
74
88
```
75
89
76
90
### ** Java**
77
91
78
92
<!-- 这里可写当前语言的特殊实现逻辑 -->
79
93
80
94
``` java
95
+ class Solution {
96
+ public int minOperations (String [] logs ) {
97
+ int ans = 0 ;
98
+ for (var v : logs) {
99
+ if (" ../" . equals(v)) {
100
+ ans = Math . max(0 , ans - 1 );
101
+ } else if (v. charAt(0 ) != ' .' ) {
102
+ ++ ans;
103
+ }
104
+ }
105
+ return ans;
106
+ }
107
+ }
108
+ ```
109
+
110
+ ### ** C++**
111
+
112
+ ``` cpp
113
+ class Solution {
114
+ public:
115
+ int minOperations(vector<string >& logs) {
116
+ int ans = 0;
117
+ for (auto& v : logs) {
118
+ if (v == "../") {
119
+ ans = max(0, ans - 1);
120
+ } else if (v[ 0] != '.') {
121
+ ++ans;
122
+ }
123
+ }
124
+ return ans;
125
+ }
126
+ };
127
+ ```
81
128
129
+ ### **Go**
130
+
131
+ ```go
132
+ func minOperations(logs []string) int {
133
+ ans := 0
134
+ for _, v := range logs {
135
+ if v == "../" {
136
+ if ans > 0 {
137
+ ans--
138
+ }
139
+ } else if v[0] != '.' {
140
+ ans++
141
+ }
142
+ }
143
+ return ans
144
+ }
82
145
```
83
146
84
147
### ** ...**
Original file line number Diff line number Diff line change 65
65
### ** Python3**
66
66
67
67
``` python
68
-
68
+ class Solution :
69
+ def minOperations (self , logs : List[str ]) -> int :
70
+ ans = 0
71
+ for v in logs:
72
+ if v == " ../" :
73
+ ans = max (0 , ans - 1 )
74
+ elif v[0 ] != " ." :
75
+ ans += 1
76
+ return ans
69
77
```
70
78
71
79
### ** Java**
72
80
73
81
``` java
82
+ class Solution {
83
+ public int minOperations (String [] logs ) {
84
+ int ans = 0 ;
85
+ for (var v : logs) {
86
+ if (" ../" . equals(v)) {
87
+ ans = Math . max(0 , ans - 1 );
88
+ } else if (v. charAt(0 ) != ' .' ) {
89
+ ++ ans;
90
+ }
91
+ }
92
+ return ans;
93
+ }
94
+ }
95
+ ```
96
+
97
+ ### ** C++**
98
+
99
+ ``` cpp
100
+ class Solution {
101
+ public:
102
+ int minOperations(vector<string >& logs) {
103
+ int ans = 0;
104
+ for (auto& v : logs) {
105
+ if (v == "../") {
106
+ ans = max(0, ans - 1);
107
+ } else if (v[ 0] != '.') {
108
+ ++ans;
109
+ }
110
+ }
111
+ return ans;
112
+ }
113
+ };
114
+ ```
74
115
116
+ ### **Go**
117
+
118
+ ```go
119
+ func minOperations(logs []string) int {
120
+ ans := 0
121
+ for _, v := range logs {
122
+ if v == "../" {
123
+ if ans > 0 {
124
+ ans--
125
+ }
126
+ } else if v[0] != '.' {
127
+ ans++
128
+ }
129
+ }
130
+ return ans
131
+ }
75
132
```
76
133
77
134
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minOperations (vector<string>& logs) {
4
+ int ans = 0 ;
5
+ for (auto & v : logs) {
6
+ if (v == " ../" ) {
7
+ ans = max (0 , ans - 1 );
8
+ } else if (v[0 ] != ' .' ) {
9
+ ++ans;
10
+ }
11
+ }
12
+ return ans;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ func minOperations (logs []string ) int {
2
+ ans := 0
3
+ for _ , v := range logs {
4
+ if v == "../" {
5
+ if ans > 0 {
6
+ ans --
7
+ }
8
+ } else if v [0 ] != '.' {
9
+ ans ++
10
+ }
11
+ }
12
+ return ans
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int minOperations (String [] logs ) {
3
+ int ans = 0 ;
4
+ for (var v : logs ) {
5
+ if ("../" .equals (v )) {
6
+ ans = Math .max (0 , ans - 1 );
7
+ } else if (v .charAt (0 ) != '.' ) {
8
+ ++ans ;
9
+ }
10
+ }
11
+ return ans ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minOperations (self , logs : List [str ]) -> int :
3
+ ans = 0
4
+ for v in logs :
5
+ if v == "../" :
6
+ ans = max (0 , ans - 1 )
7
+ elif v [0 ] != "." :
8
+ ans += 1
9
+ return ans
You can’t perform that action at this time.
0 commit comments