-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexclusiveTime.cpp
More file actions
32 lines (31 loc) · 1.13 KB
/
exclusiveTime.cpp
File metadata and controls
32 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<int> exclusiveTime(int n, vector<string>& logs) {
stack<pair<int, int>> st;
vector<int> res(n, 0);
for (auto& log : logs) {
char type[10];
int idx, timestamp;
sscanf(log.c_str(), "%d:%[^:]:%d", &idx, type, ×tamp);
if (type[0] == 's') {
if (!st.empty()) {
res[st.top().first] += timestamp - st.top().second;
st.top().second = timestamp;
}
st.emplace(idx, timestamp);
} else {
auto t = st.top();
st.pop();
res[t.first] += timestamp - t.second + 1;
if (!st.empty()) {
st.top().second = timestamp + 1;
}
}
}
return res;
}
};
// 作者:LeetCode-Solution
// 链接:https://leetcode.cn/problems/exclusive-time-of-functions/solution/han-shu-de-du-zhan-shi-jian-by-leetcode-d54e2/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。