636. Exclusive Time of Functions
每次都把区间放进去, 要是碰到end,就结束上一个start的区间
1 class Solution { 2 public int[] exclusiveTime(int n, List<String> logs) { 3 if(logs.size() == 0) return new int[0]; 4 int[] res = new int[n]; 5 Stack<Integer> stack = new Stack<>(); 6 int prev = 0; 7 for(String log : logs){ 8 String[] strs = log.split(":"); 9 int id = Integer.parseInt(strs[0]); 10 int time = Integer.parseInt(strs[2]); 11 if(!stack.isEmpty()) res[stack.peek()] += time - prev; 12 prev = time; 13 if(strs[1].equals("start")){ 14 stack.push(id); 15 }else{ 16 res[stack.pop()]++; //注意因为是1秒区间 所以要加1 17 prev++; 18 } 19 } 20 return res; 21 } 22 }