Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int scoreOfParentheses(string S) {
- stack<char> p;
- stack<int> v;
- for (int i = 0; i < S.size(); ++i) {
- if (S[i] == '(') {
- p.push('(');
- } else {
- int total = 0;
- while (p.top() != '(') {
- total += v.top();
- p.pop();
- v.pop();
- }
- if (total > 0) {
- v.push(total * 2);
- } else {
- v.push(1);
- }
- p.pop();
- p.push('*');
- }
- }
- int sum = 0;
- while (!v.empty()) {
- sum += v.top();
- v.pop();
- }
- return sum;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement