0636.cpp (640B)
1 class Solution { 2 public: 3 vector<int> exclusiveTime(int n, const vector<string> &logs) { 4 stack<pair<int, int>> st; 5 vector<int> res(n, 0); 6 int time, id; 7 char op[6]; 8 9 for (const string &log : logs) { 10 sscanf(log.c_str(), "%d:%[^:]:%d", &id, op, &time); 11 if (!strcmp(op, "start")) 12 st.push({id, time}); 13 else { 14 const int added = time - st.top().second + 1; 15 res[id] += added; 16 st.pop(); 17 18 if (!st.empty()) res[st.top().first] -= added; 19 } 20 } 21 return res; 22 } 23 };