1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public int[] exclusiveTime(int n, List<String> logs) { int[] res = new int[n]; Stack<Integer> st = new Stack<>(); int pre = 0; for (String log : logs) { String[] arr = log.split(":"); if (arr[1].equals("start")) { if (!st.empty()) res[st.peek()] += Integer.parseInt(arr[2]) - pre; st.push(Integer.parseInt(arr[0])); // res: index pre = Integer.parseInt(arr[2]); } else { res[st.pop()] += Integer.parseInt(arr[2]) - pre + 1; pre = Integer.parseInt(arr[2]) + 1; } } return res; }
|