12周刷题大作战

之前的计划是每周六休息,鉴于前两天太乏提前休息,今天开始再次做题。

LeetCode - 636, 71, 385 (0 Easy, 3 Medium, 0 Hard)

636. Exclusive Time of Functions - Medium

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
class  {
public int[] exclusiveTime(int n, List<String> logs) {
Stack<Node> stack = new Stack<>();
int[] result = new int[n];

for(String log: logs){
String[] content = log.split(":");
int id = Integer.valueOf(content[0]);
int time = Integer.valueOf(content[2]);

if(content[1].equals("start")){
stack.push(new Node(id, time));
}else{
Node cur = stack.pop();
int consumed = time - cur._start + 1 - cur._consumedByChildren;
result[id] += consumed;
if(!stack.isEmpty()){
stack.peek()._consumedByChildren += time - cur._start + 1;
}
}
}
return result;
}

private class Node{
final int _id;
final int _start;
int _consumedByChildren;

public Node(int id, int start){
_id = id;
_start = start;
_consumedByChildren = 0;
}
}
}

71. Simplify Path - Medium

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
class  {
public String simplifyPath(String path) {
Stack<String> stack = new Stack<>();


int start = 0;
String str = new String();
for(int i=1; i<path.length(); i++){
char cur = path.charAt(i);
if(cur == '/'){
if(path.charAt(i-1) != '/'){
str = path.substring(start, i);
if(str.equals("/..") && !stack.isEmpty()){
stack.pop();
}else if(!str.equals("/.") && !str.equals("/..")){
stack.push(str);
}
}
start = i;
}
}
String strEnd = path.substring(start, path.length());

if(stack.isEmpty()){
if(strEnd.equals("/..") || strEnd.equals("/.")){
stack.push("/");
}else{
stack.push(strEnd);
}
}else{
if(strEnd.equals("/..")){
stack.pop();
if(stack.isEmpty()){
stack.push("/");
}
}else if(!strEnd.equals("/") && !strEnd.equals("/.")){
stack.push(strEnd);
}
}


StringBuilder sb = new StringBuilder();
for(String s: stack){
sb.append(s);
}
return sb.toString();
}
}

385 - Mini Parser - Medium

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
62
63
64
65
66
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
class {
public NestedInteger deserialize(String s) {
Stack<NestedInteger> stack = new Stack<>();
if(s.charAt(0) != '['){
return new NestedInteger(Integer.valueOf(s));
}
NestedInteger fakeBaba = new NestedInteger();
int start = 0;
stack.push(fakeBaba);
for(int i=0; i < s.length(); i++){
char c = s.charAt(i);
if( c == '-' || '0'<= c && c <= '9'){
continue;
}
if(c == '['){
NestedInteger nested = new NestedInteger();
stack.peek().add(nested);
stack.push(nested);
}else{
if(s.charAt(i-1) >= '0' && s.charAt(i-1) <= '9'){
int num = Integer.valueOf(s.substring(start, i));
NestedInteger nested = new NestedInteger(num);
stack.peek().add(nested);
}
if(c == ']'){
stack.pop();// '['时,既peek又push了,所以当']'或','时 给peek加就相当于给前一个也加了;
}
}


start = i+1;

}

return stack.peek().getList().get(0);

}
}