-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack636.java
More file actions
61 lines (53 loc) · 1.78 KB
/
Stack636.java
File metadata and controls
61 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package stack;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Stack636 {
public static void main(String[] args) {
int n = 1;
List logs = Arrays.asList(new String[] {
"0:start:0","0:start:1","0:start:2","0:end:3","0:end:4","0:end:5"
});
exclusiveTime(n, logs);
}
/**
* 执行用时 :160 ms, 在所有 java 提交中击败了5.00%的用户
* 内存消耗 :39.2 MB, 在所有 java 提交中击败了94.29%的用户
* @param n
* @param logs
* @return
*/
public static int[] exclusiveTime(int n, List<String> logs) {
int[] result = new int[n];
Stack<String> stack = new Stack<>();
for (String log:logs) {
if (isEnd(log)) {
int endTime = getTime(log);
int otherTime = 0;
while (!stack.isEmpty() && !isStart(stack.peek())) {
otherTime += getTime(stack.pop());
}
int useTime = endTime - getTime(stack.pop()) + 1;
result[getFunction(log)] += useTime - otherTime;
stack.push(getFunction(log) + ":use:" + useTime);
System.out.println(result[0]);
System.out.println(stack);
} else {
stack.push(log);
}
}
return result;
}
public static int getTime(String log) {
return Integer.parseInt(log.split(":")[2]);
}
public static int getFunction(String log) {
return Integer.parseInt(log.split(":")[0]);
}
public static boolean isStart(String log) {
return log.split(":")[1].equals("start");
}
public static boolean isEnd(String log) {
return log.split(":")[1].equals("end");
}
}