---
# System prepended metadata

title: 0636. Exclusive Time of Functions
tags: [Leetcode, FaceBook, Medium]

---

# 0636. Exclusive Time of Functions
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/exclusive-time-of-functions/
## 思路
用stack存取状态们
## Code
```java=
class Solution {
    public int[] exclusiveTime(int n, List<String> logs) {
        int[] res = new int[n];
        Stack<Integer> stack = new Stack<>();
        String[] s = logs.get(0).split(":");
        stack.push(Integer.parseInt(s[0]));
        int prev = Integer.parseInt(s[2]);
        for(int i = 1;i < logs.size();i++){
            s = logs.get(i).split(":");
            int num = Integer.parseInt(s[0]);
            int timeStamp = Integer.parseInt(s[2]);
            if(s[1].equals("start")){
                if(!stack.empty()) {
                    res[stack.peek()] += timeStamp - prev;
                }
                prev = timeStamp;
                stack.push(num);
            }
            else{
                res[num]+=timeStamp-prev+1;
                stack.pop();
                prev = timeStamp+1;
            }
        }
        return res;
    }
}
```