-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (42 loc) · 1.28 KB
/
Copy pathSolution.java
File metadata and controls
52 lines (42 loc) · 1.28 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
package stack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
class Solution {
public int scoreOfParentheses(String s) {
if(null == s || s.length() < 1 || s.length() % 2 != 0){
return 0;
}
Stack<Character> stack = new Stack<Character>();
char[] charArray = s.toCharArray();
for (Character k : charArray){
if('(' == k){
stack.push(k);
}else{
int score;
//开始计算分数
char c = stack.pop();
if(c == '('){
score = 1;
}else{
score = (c - '0')*2;
if(!stack.isEmpty()){
stack.pop();//把'('也弹出去
}
}
if(!stack.isEmpty() && stack.peek() != '('){
score += (stack.pop()-'0');
}
stack.push((char)(score+'0'));
}
}
return stack.pop() - '0';
}
public static void main(String[] args) {
int i = new Solution().scoreOfParentheses("(()(()))");
System.out.println(i);
}
List<Character> symbel = new ArrayList() { {
}};
}