Skip to content

Commit ef47862

Browse files
committed
solve problem Score Of Parentheses
1 parent b6877ce commit ef47862

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ All solutions will be accepted!
268268
|814|[Binary Tree Pruning](https://leetcode-cn.com/problems/binary-tree-pruning/description/)|[java/py/js](./algorithms/BinaryTreePruning)|Medium|
269269
|150|[Evaluate Reverse Polish Notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/description/)|[java/py/js](./algorithms/EvaluateReversePolishNotation)|Medium|
270270
|341|[Flatten Nested List Iterator](https://leetcode-cn.com/problems/flatten-nested-list-iterator/description/)|[java/py/js](./algorithms/FlattenNestedListIterator)|Medium|
271+
|856|[Score Of Parentheses](https://leetcode-cn.com/problems/score-of-parentheses/description/)|[java/py/js](./algorithms/ScoreOfParentheses)|Medium|
271272

272273
# Database
273274
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Score Of Parentheses
2+
We can solve this problem by stack
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int scoreOfParentheses(String S) {
3+
List<String> stack = new ArrayList<String>();
4+
5+
for (int i = 0; i < S.length(); i++) {
6+
String c = S.substring(i, i + 1);
7+
8+
int size = stack.size();
9+
if (c.equals("("))
10+
stack.add(c);
11+
else if (stack.get(size - 1).equals("("))
12+
stack.set(size - 1, String.valueOf(1));
13+
else
14+
stack.set(size - 2, String.valueOf(2 * Integer.valueOf(stack.remove(size - 1))));
15+
16+
int v = 0;
17+
while (stack.size() > 0 && !stack.get(stack.size() - 1).equals("("))
18+
v += Integer.valueOf(stack.remove(stack.size() - 1));
19+
if (v > 0)
20+
stack.add(String.valueOf(v));
21+
}
22+
23+
return stack.size() > 0 ? Integer.valueOf(stack.get(0)) : 0;
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} S
3+
* @return {number}
4+
*/
5+
var scoreOfParentheses = function(S) {
6+
let stack = []
7+
8+
for (let i = 0, c = S[i]; i < S.length; i++, c = S[i]) {
9+
let length = stack.length
10+
11+
if (c == '(')
12+
stack.push(c)
13+
else if (stack[length - 1] == '(')
14+
stack[length - 1] = 1
15+
else
16+
stack[length - 2] = 2 * stack.pop()
17+
18+
let v = 0
19+
while (stack.length > 0 && stack[stack.length - 1] != '(')
20+
v += stack.pop()
21+
if (v > 0)
22+
stack.push(v)
23+
}
24+
25+
return stack.length > 0 ? stack[0] : 0
26+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def scoreOfParentheses(self, S):
3+
"""
4+
:type S: str
5+
:rtype: int
6+
"""
7+
stack = []
8+
9+
for c in S:
10+
if c == '(':
11+
stack.append(c)
12+
elif stack[-1] == '(':
13+
stack[-1] = 1
14+
else:
15+
stack[-2] = 2 * stack[-1]
16+
stack.pop()
17+
18+
v = 0
19+
while len(stack) > 0 and stack[-1] != '(':
20+
v += stack.pop()
21+
if v > 0:
22+
stack.append(v)
23+
24+
return 0 if len(stack) == 0 else stack[0]

0 commit comments

Comments
 (0)