1. 程式人生 > >856. Score of Parentheses

856. Score of Parentheses

1,題目要求
Given a balanced parentheses string S, compute the score of the string based on the following rule:
這裡寫圖片描述
對於給定的括號序列,計算對應的最後的結果。

2,題目思路
對於這道題,是括號匹配問題的一個變形:
一個空的括號匹配代表1;
內部有數字的匹配代表內部的2倍;
連續的兩個匹配代表相加的和;
因此,對於這樣的問題,一般都是利用來實現的。
在具體實現時,根據是否匹配以及此時是否有棧頂元素,來進行相應的資料判斷。

3,程式原始碼

class Solution {
public
: int scoreOfParentheses(string S) { stack<int> s; for(auto c : S) { if(c == '(') s.push(-1); else { int curr = 0; while(s.top()!=-1) { curr += s.top(); s.pop(); } s.pop(); s.push(curr == 0
? 1 : 2*curr); } } int res = 0; while(!s.empty()) { res += s.top(); s.pop(); } return res; } };

相關推薦

Leetcode 856. Score of Parentheses 括號得分(棧)

Leetcode 856. Score of Parentheses 括號得分(棧) 題目描述 字串S包含平衡的括號(即左右必定匹配),使用下面的規則計算得分 () 得1分 AB 得A+B的分,比如()()得2分 (A) 得2A分, 比如(()())得2(1+1)分 測試樣例 Ex

856. Score of Parentheses(python+cpp)

題目: 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,

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

【LeetCode】856. Score of Parentheses 解題報告(Python & C++)

作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 棧 遞迴 計數 日期

856. Score of Parentheses

1,題目要求 Given a balanced parentheses string S, compute the score of the string based on the following

Score of Parentheses(856)

856— Score of Parentheses Given a balanced parentheses string S, compute the score of the string based on the following rule: “()” has sco

js做演算法題-Score of Parentheses

字母移位 題目描述: 給定一個平衡的括號字串S,根據以下規則計算字串的得分: ()有1分 AB有得分A + B,其中A和B是平衡的括號字串。 (A)有得分2 * A,其中A是一個平衡的括號字串。 例1: 輸入:"()" 輸出:1 例2:

Score of Parentheses 括號的分數

給定一個平衡括號字串 S,按下述規則計算該字串的分數: () 得 1 分。 AB 得 A + B 分,其中 A 和 B 是平衡括號字串。 (A) 得 2 * A 分,其中 A 是平衡括號字串。 示例 1: 輸入: "()" 輸出: 1 示例 2: 輸入: "((

[LeetCode] Score of Parentheses 括號的分數

參考 string http https balanced tput HERE win wing Given a balanced parentheses string?S, compute the score of the string based on the fol

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.&n

The score of 'O' and 'X'

題目描述 注意要點: 使用strlen函式注意加標頭檔案#inlcude <cstring> 迴圈巨集定義for迴圈#define _for(i,a,b) for(int i=(a);i<(b);++i) 程式碼實現 #include <iostream>

[GeeksForGeeks] Count Number of ways to reach a given score in a game

oid it is col class back put () version following Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total s

Leetcode-5047 Minimum Score Triangulation of Polygon(多邊形三角剖分的最低得分)

long int class code amp turn eight con lee 1 const int maxn = 10000; 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 type

[LeetCode]160.Intersection of Two Linked Lists

col style return tro nod sts diff original you Intersection of Two Linked Lists Write a program to find the node at which the intersectio

IOC Of Ninject Base On ASP.NET MVC

dex 準備工作 應用 new 引用 cti err art part 說在之前的話 IOC的概念相信大家比較熟悉了,習慣性稱之為依賴註入或控制反轉,園子裏對基於MVC平臺IOC設計模式已經相當多了,但大家都只知道應該怎麽應用一個IOC模式,比如Ninject, Unity

codeforce 804B Minimum number of steps

bst 代碼 img 添加 close perf rep spl pri cf勁啊 原題: We have a string of letters ‘a‘ and ‘b‘. We want to perform some operations on it. On each

Principle of Computing (Python)學習筆記(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

ide out generate depth sku color ati cond with 1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/prin

LeetCode Length of Last Word

archive n-1 art fine ive lan ets hello style 1. 題目Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

給他 cas 維數 color cost 代碼 01背包 size code HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=3591 題意:

Codeforces466C Number of Ways

ace ems ring tin ons 代碼 節點 -- 題意 題目鏈接: http://codeforces.com/problemset/problem/466/C 題意: 給一個長度為n的數組,將其分成連續的三段使三段的和相等。求有幾種這種組合 分析: