C++ Concise Solution


  • 0
    Z
    class Solution {
    public:
        vector<int> exclusiveTime(int n, vector<string>& logs) {
            vector<int> ret(n, 0);
            stack<int> st;
            int prev_t = 0;
            char tmp[16]; 
            for (string s : logs) {
                stringstream ss(s);
                ss.getline(tmp, 16, ':');
                int id = stoi(tmp);
                ss.getline(tmp, 16, ':');
                bool flag = (tmp[0] == 'e');
                ss.getline(tmp, 16, ':');
                int t = stoi(tmp);
                
                if (st.empty()) {
                    st.push(id);
                    prev_t = t;
                } else {
                    ret[st.top()] += t - prev_t;
                    prev_t = t;
                    if (flag) {
                        ++ret[st.top()];
                        ++prev_t;
                        st.pop();
                    } else st.push(id);
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.