856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule:

  • () has score 1
  • AB has score A + B, where A and B are balanced parentheses strings.
  • (A) has score 2 * A, where A is a balanced parentheses string.
Example 1:
Input: "()"
Output: 1

Example 2:
Input: "(())"
Output: 2

Example 3:
Input: "()()"
Output: 2

Example 4:
Input: "(()(()))"
Output: 6

计算由"("和")"组成的字符串的值。嵌套:乘2,并列:相加

用stack就可以。"("入栈,")",往前搜索第一个"(",如果中间有数字的,取出数字求和并乘以2;如果没有数字,直接压入"1"。

最后把stack中所有的数字相加。

 1 class Solution {
 2 public:
 3     int scoreOfParentheses(string S) {
 4         stack<string> s;
 5         int i = 0;
 6         while (i < S.length()) {
 7             if (S[i] == ‘(‘)
 8                 s.push("(");
 9             else {
10                 if (s.top() == "(") {
11                     s.pop();
12                     s.push("1");
13                 }
14                 else {
15                     int temp = 0;
16                     while (s.top() != "(") {
17                         temp += stoi(s.top());
18                         s.pop();
19                     }
20                     s.pop();
21                     s.push(to_string(2 * temp));
22                 }
23             }
24             ++i;
25         }
26         int res = 0;
27         while (!s.empty()) {
28             res += stoi(s.top());
29             s.pop();
30         }
31         return res;
32     }
33 };

原文地址:https://www.cnblogs.com/Zzz-y/p/9221472.html

时间: 2024-10-31 03:14:00

856. Score of Parentheses的相关文章

Leetcode 856. Score of Parentheses 括号得分(栈)

Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如()()得2分 (A) 得2A分, 比如(()())得2(1+1)分 测试样例 Example 1: Input: "()" Output: 1 Example 2: Input: "(())" Output: 2 Example 3: Input: "()(

LC 856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule: () has score 1 AB has score A + B, where A and B are balanced parentheses strings. (A) has score 2 * A, where A is a balanced parentheses string. Exam

leetcode 856. Score of Parentheses

Given a balanced parentheses string S, compute the score of the string based on the following rule: () has score 1 AB has score A + B, where A and B are balanced parentheses strings. (A) has score 2 * A, where A is a balanced parentheses string. Exam

leetcode 856. 括号的分数(Score of Parentheses)

目录 题目描述: 示例 1: 示例 2: 示例 3: 示例 4: 解法: 题目描述: 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A 和 B 是平衡括号字符串. (A) 得 2 * A 分,其中 A 是平衡括号字符串. 示例 1: 输入: "()" 输出: 1 示例 2: 输入: "(())" 输出: 2 示例 3: 输入: "()()" 输出: 2 示例 4: 输入: &quo

[LeetCode] Score of Parentheses 括号的分数

Given a balanced parentheses string?S, compute the score of the string based on the following rule: ()?has score 1 AB?has score?A + B, where A and B are balanced parentheses strings. (A)?has score?2 * A, where A is a balanced parentheses string. Exam

20/32/22/856/301/921 Parentheses 括号匹配或者生成题

20. https://leetcode.com/problems/valid-parentheses/description/ 32. https://leetcode.com/problems/longest-valid-parentheses/description/ 22. https://leetcode.com/problems/generate-parentheses/description/ 856. https://leetcode.com/problems/score-of-

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

2014-11-9------- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。

一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示.用SQL语句创建四个表并完成相关题目. 表1-1数据库的表结构 表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno Char(3) 否 学号(主码) Sname Char(8) 否 学生姓名 Ssex Char(2) 否