Crawler Log Folder LeetCode solution in C++

1
// 1.using stack
2
class Solution {
3
public:
4
int minOperations(vector<string> &logs) {
5
if (logs.size() == 0) return 0;
6

7
stack<string> st;
8
for (auto x : logs) {
9
if (x[0] != '.') // Move to the child folder so add children
10
st.push(x);
11
else if (x == "../") { // Move to the parent folder of the current folder
12
// so pop
13
if (!st.empty())
14
st.pop();
15
else
16
continue; // don’t move the pointer beyond the main folder.
17
}
18
}
19
return st.size();
20
}
21
};
22
// 2.
23
class Solution {
24
public:
25
int minOperations(vector<string> &logs) {
26
int ans = 0;
27
for (string log : logs) {
28
if (log == "../") { // go deeper
29
ans--;
30
ans = max(ans, 0);
31
} else if (log != "./") // one level up
32
ans++;
33
}
34
return ans;
35
}
36
};
37
// 3.
38
class Solution {
39
public:
40
int minOperations(vector<string> &logs) {
41
int res = 0;
42
for (string s : logs) {
43
if (s == "../")
44
res = max(0, --res);
45
else if (s == "./")
46
continue;
47
else
48
res++;
49
}
50
return res;
51
}
52
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0