-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack856.java
More file actions
72 lines (68 loc) · 1.66 KB
/
Stack856.java
File metadata and controls
72 lines (68 loc) · 1.66 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
62
63
64
65
66
67
68
69
70
71
72
package stack;
import java.util.Stack;
/**
* @ProjectName: leetcode
* @Package: stack
* @ClassName: Stack856
* @Author: markey
* @Description:
* 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:
*
* () 得 1 分。
* AB 得 A + B 分,其中 A 和 B 是平衡括号字符串。
* (A) 得 2 * A 分,其中 A 是平衡括号字符串。
*
*
* 示例 1:
*
* 输入: "()"
* 输出: 1
* 示例 2:
*
* 输入: "(())"
* 输出: 2
* 示例 3:
*
* 输入: "()()"
* 输出: 2
* 示例 4:
*
* 输入: "(()(()))"
* 输出: 6
*
*
* 提示:
*
* S 是平衡括号字符串,且只含有 ( 和 ) 。
* 2 <= S.length <= 50
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/score-of-parentheses
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/10/18 0:23
* @Version: 1.0
*/
public class Stack856 {
/**
* 执行用时 :1 ms, 在所有 java 提交中击败了89.63%的用户
* 内存消耗 :33.9 MB, 在所有 java 提交中击败了89.09%的用户
* @param S
* @return
*/
public int scoreOfParentheses(String S) {
char[] chars = S.toCharArray();
Stack<Integer> stack = new Stack<>();
stack.push(0);
int temp;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '(') {
stack.push(0);
} else {
int v = stack.pop();
int m = stack.pop();
stack.push(m + Math.max(v * 2, 1));
}
}
return stack.pop();
}
}