LeetCode 856. 括号的分数

解题思路

栈模拟。

相关代码

复制代码
class Solution {
    public int scoreOfParentheses(String s) {
        //stack中的值是左括号的的右边所有合法配对括号的值
        Stack<Integer> stack = new Stack<>();
        stack.push(0);
        for(int i=0;i<s.length();i++)
            if(s.charAt(i) == '(') stack.push(0);           
            else{
                int t = stack.pop();
                int k = stack.pop();
                if(t == 0) stack.push(k+t+1);
                else stack.push(k+t*2);
            }    
        return stack.peek();
    }
}
相关推荐
星释21 小时前
Rust 练习册 :Pythagorean Triplet与数学算法
开发语言·算法·rust
星释21 小时前
Rust 练习册 :Nth Prime与素数算法
开发语言·算法·rust
多喝开水少熬夜1 天前
Trie树相关算法题java实现
java·开发语言·算法
WBluuue1 天前
数据结构与算法:树上倍增与LCA
数据结构·c++·算法
bruk_spp1 天前
牛客网华为在线编程题
算法
黑屋里的马1 天前
java的设计模式之桥接模式(Bridge)
java·算法·桥接模式
sin_hielo1 天前
leetcode 1611
算法·leetcode
李小白杂货铺1 天前
识别和破除信息茧房
算法·信息茧房·识别信息茧房·破除信息茧房·算法推荐型茧房·观点过滤型茧房·茧房
来荔枝一大筐1 天前
C++ LeetCode 力扣刷题 541. 反转字符串 II
c++·算法·leetcode
暴风鱼划水1 天前
算法题(Python)数组篇 | 6.区间和
python·算法·数组·区间和