题目描述
思路
模拟
直接模拟即可。
Python实现
class Solution:
def minOperations(self, logs: List[str]) -> int:
depth = 0
for log in logs:
if log == './':
continue
if log != '../':
depth += 1
elif depth:
depth -= 1
return depth
Java实现
class Solution {
public int minOperations(String[] logs) {
int depth = 0;
for (String log : logs) {
if ("./".equals(log)) {
continue;
} else if ("../".equals(log)) {
if (depth > 0) {
depth--;
}
} else {
depth++;
}
}
return depth;
}
}
C++实现
class Solution {
public:
int minOperations(vector<string>& logs) {
int depth = 0;
for (auto& log : logs) {
if (log == "./") {
continue;
} else if (log == "../") {
if (depth > 0) {
depth--;
}
} else {
depth++;
}
}
return depth;
}
};