- public int[] exclusiveTime(int n, List<String> logs) {
- int[] ans=new int[n];
- Deque<int[]> s=new ArrayDeque<>();//模拟当前调用
- Deque<int[]> s1=new ArrayDeque<>();//存储已结束函数的起止点,包括同层函数与内部函数
- for(String log:logs){
- String[] ss=log.split(":");
- if(ss[1].equals("start")){//调用起点入栈
- s.add(new int[]{Integer.parseInt(ss[0]),Integer.parseInt(ss[2])});
- }else{
- int[] tmp=s.pollLast();
- int time=Integer.parseInt(ss[2])-tmp[1]+1;//当前结束函数的调用间隔
- while(!s1.isEmpty()&&s1.getLast()[0]>=tmp[1]){
- int[] tmp1=s1.pollLast();
- time-=tmp1[1]-tmp1[0]+1;//判断已结束函数是否位于当前结束函数内部
- }
- ans[tmp[0]]+=time;//累加时间
- s1.add(new int[]{tmp[1],Integer.parseInt(ss[2])});//将当前结束函数加入栈
- }
- }
- return ans;
- }