File tree 5 files changed +78
-0
lines changed
algorithms/ScoreOfParentheses
5 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -268,6 +268,7 @@ All solutions will be accepted!
268
268
| 814| [ Binary Tree Pruning] ( https://leetcode-cn.com/problems/binary-tree-pruning/description/ ) | [ java/py/js] ( ./algorithms/BinaryTreePruning ) | Medium|
269
269
| 150| [ Evaluate Reverse Polish Notation] ( https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/description/ ) | [ java/py/js] ( ./algorithms/EvaluateReversePolishNotation ) | Medium|
270
270
| 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|
271
272
272
273
# Database
273
274
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Score Of Parentheses
2
+ We can solve this problem by stack
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments