Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit30f15e2

Browse files
refactor 636
1 parent65a8987 commit30f15e2

File tree

1 file changed

+28
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-24
lines changed

‎src/main/java/com/fishercoder/solutions/_636.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,35 @@
4141
1 <= n <= 100
4242
*/
4343
publicclass_636 {
44-
/**Based on the example, it's difficult to see how function 2 executes 4 units of time, actually
45-
* we can add 1 to all end times to make it easier to understand and AC'ed.*/
46-
publicint[]exclusiveTime(intn,List<String>logs) {
47-
/**Stack is the way to go:
48-
* we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
49-
* we'll pop this logId only when we encounter this logId's end timestamp.
50-
* Meanwhile, we keep a counter called prevTime,
51-
* whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
52-
Deque<Integer>stack =newLinkedList<>();
53-
int[]result =newint[n];
54-
intprevTime =0;
55-
for (Stringlog :logs) {
56-
String[]parts =log.split(":");
57-
if (!stack.isEmpty()) {
58-
result[stack.peek()] +=Integer.parseInt(parts[2]) -prevTime;
59-
}
60-
prevTime =Integer.parseInt(parts[2]);
61-
if (parts[1].equals("start")) {
62-
stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push()
63-
}else {
64-
prevTime++;
65-
//remember to have result pluse 1 to match the problem AC criteria
66-
result[stack.pollFirst()]++;//i.e. stack.pop()
44+
publicstaticclassSolution1 {
45+
/**
46+
* Based on the example, it's difficult to see how function 2 executes 4 units of time, actually
47+
* we can add 1 to all end times to make it easier to understand and AC'ed.
48+
*/
49+
publicint[]exclusiveTime(intn,List<String>logs) {
50+
/**Stack is the way to go:
51+
* we keep pushing the logId onto the stack whenever we just encounter this logId's start timestamp,
52+
* we'll pop this logId only when we encounter this logId's end timestamp.
53+
* Meanwhile, we keep a counter called prevTime,
54+
* whenever the stack is not empty, we'll always deduct prevTime from the last logId on the stack.*/
55+
Deque<Integer>stack =newLinkedList<>();
56+
int[]result =newint[n];
57+
intprevTime =0;
58+
for (Stringlog :logs) {
59+
String[]parts =log.split(":");
60+
if (!stack.isEmpty()) {
61+
result[stack.peek()] +=Integer.parseInt(parts[2]) -prevTime;
62+
}
63+
prevTime =Integer.parseInt(parts[2]);
64+
if (parts[1].equals("start")) {
65+
stack.addFirst(Integer.parseInt(parts[0]));//i.e. stack.push()
66+
}else {
67+
prevTime++;
68+
//remember to have result pluse 1 to match the problem AC criteria
69+
result[stack.pollFirst()]++;//i.e. stack.pop()
70+
}
6771
}
72+
returnresult;
6873
}
69-
returnresult;
7074
}
7175
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp