|
|
谢谢分享!
- public class Solution {
- public int[] exclusiveTime(int n, List<String> logs) {
- int[] exclusiveTime = new int[n];
- Stack<Integer> stack = new Stack<>();
- int prevTime = 0;
- for (String log : logs) {
- String[] parts = log.split(":");
- int functionId = Integer.parseInt(parts[0]);
- String action = parts[1];
- int timestamp = Integer.parseInt(parts[2]);
- if (action.equals("start")) {
- if (!stack.isEmpty()) {
- exclusiveTime[stack.peek()] += timestamp - prevTime;
- }
- stack.push(functionId);
- prevTime = timestamp; // Update prevTime
- } else {
- exclusiveTime[stack.pop()] += timestamp - prevTime + 1;
- prevTime = timestamp + 1; // Update prevTime
- }
- }
- return exclusiveTime;
- }
- }
复制代码 |
|