[LeetCode]32. Longest Valid Parentheses

ChipCamp探索系列 -- 7A. 开源CPU之BOOM的来龙去脉 本文梳理一下Berkeley的乱序超标量CPU项目----Berkeley Out of Order Machine (BOOM),作为探索7系的首篇(7A)。 阅读详情

[LeetCode]32. Longest Valid Parentheses

题目描述

这里写图片描述

思路

开始:将’(‘压栈,当有’)’时, 出栈,结果+1
问题:统计的是所有合法的情况,不是最长合法的情况
修正:将对应的索引压栈,满足条件时,出栈,当前索引减去栈顶索引,栈初始化时先将-1压栈

代码

class Solution {
public:
    int longestValidParentheses(string s) {
        int res = 0;
        stack<int> index;
        index.push(-1);
        for (int i = 0; i < s.size(); ++i) {
            if (index.top() != -1 && s[index.top()] == '(' && s[i] == ')'){
                index.pop();
                res = res > i - index.top() ? res : i - index.top();
            }
            else {
                index.push(i);
            }
        }
        return res;  
    }
};
Matlab+Matpower实战:手把手教你生成IEEE14节点FDIA攻击数据集(附完整代码) 本文提供了一份基于Matlab与Matpower工具构建IEEE14节点FDIA攻击数据集的完整实战指南。通过动态负荷数据模拟、潮流计算、状态估计及隐蔽攻击向量注入,手把手教你生成可用于电力系统网络安全研究的仿真数据集,并附有核心代码与调试技巧。 阅读详情

相关推荐

设备接入与APP(应用程序)接入华为云iotDA平台的详细操作步骤及获取方式

设备及APP(应用程序) 登录 IoTDA 平台所需的 入场与操作清单内容,提供逐项详细操作步骤和获取方式,并穿插贴士便于理解与实际操作落地。内容基于 华为云 IoTDA 控制台 和 IAM 控制台 的实际使用路径整理。

hy行者勇哥的博客 2985

leetcode记录32.Longest Valid Parentheses

leetcode记录32.Longest Valid Parentheses hard Runtime: 8 ms, faster than 98.79% of C++ online submissions for Longest Valid Parentheses. Memory Usage: 10.6 MB, less than 80.80% of C++ online submissions...

我的博客 191

【CAN总线测试】——通讯相关诊断测试

本章主要介绍通讯相关诊断测试用例设计

LOVE135149的博客 1780

LeetCode 32. Longest Valid Parentheses

问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度。 解题思路 括号匹配问题一般借助 栈,便于理解。定义 \(start\) 记录合法字符串的起始位置,遍历字符串: 当遇到左括号,则把其索引压入栈中; 当遇到右括号,判断栈是否为空,若为空,说明当前右括号无法匹配,则需要更新合法开始位置为i+1...

weixin_30906701的博客 98

Leetcode32. Longest Valid Parentheses

题目地址: https://leetcode.com/problems/longest-valid-parentheses/

数学、算法爱好者的博客 350

leetcode 32. Longest Valid Parentheses

leetcode 32. Longest Valid Parentheses  奇了怪了,为什么java版本的算法TLE,c++版本的代码acc,难到是两种语言本身的区别:String 以及 stack操作 java版本的: package my; import java.util.Stack; public class Solution { public stat

独钓寒江雪 380

Leetcode——32. Longest Valid Parentheses

一、题目描述 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

parishong的博客 424

[Java] LeetCode32 Longest Valid Parentheses

[Java] LeetCode32 Longest Valid Parentheses

fumier的专栏 985

leetcode32——Longest Valid Parentheses

题目大意:给出由左右小括号组成的字符串,找出最长的合法字符串长度 分析:暴力解法或者dp。 暴力:枚举起始点,然后记录左右括号个数进行判断。 dp:状态:dp[i]——以下标i结尾的最长合法长度 初始化:dp[i]=0 结果:max{dp[i]} ...

tzyshiwolaogongya的博客 234

[Leetcode] 32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",

Matthew的练习场 508

Leetcode 32 Longest Valid Parentheses DP好题

就喜欢做这种想法题, 一开始的想法和之前做过的一道括号匹配差不多,用栈。 记录未匹配的括号位置,然后两两相减找到最大值,过了,但速度并不快。 在discuss中看到有人用DP,恍然大悟! dp[i]表示以当前位置为终点的最长长度,则只能在)处更新, 如果s[i-1-dp[i-1]]=='(',则说明当前位置可以和i-1-dp[i-1]位置匹配,dp[i]=dp[i-1]+2; 然后还要加上匹配位置之前的最长长度dp[i]+=dp[i-dp[i]];

无名山丘,崛起成峰 4086

LeetCode(32)Longest Valid Parentheses

题目如下: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",

BUILD 2770

LeetCode 32. Longest Valid Parentheses 题解

本题是括号匹配问题的进阶版本。栈方法:记录索引,计算长度动态规划:状态定义和转移方程处理不同的括号匹配情况。

cannonjinx的博客 2276

Leetcode 32 Longest Valid Parentheses

思路:brute force。暴力解法,找出所有的substring,然后判断是否valid。判断方法在Leetcode 20 Valid Parentheses。这里再介绍一种方法,用stack,遇到开括号push开括号,遇到闭括号pop在stack中的开括号。在这整个过程中,stack为空时,这时候再遇到闭括号,就直接返回false。或者过程结束后,stack中不为空,也返回false。除去了这两种情况,返回true。下面给出代码: class Solution { //该方法没问题,

GoodJobJasper的博客 239

[LeetCode] 32. Longest Valid Parentheses

[LeetCode] 32. Longest Valid Parentheses 问题描述 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. 思路 – dp dp[i]表示以第i个字符...

F_AnZzz的博客 217

LeetCode 32. Longest Valid Parentheses(栈)

题目来源:https://leetcode.com/problems/longest-valid-parentheses/ 问题描述 32.Longest Valid Parentheses Hard Given a string containing just the characters'('and')', find the length of the longest vali...

da_kao_la的博客 177

leetcode32Longest Valid Parentheses

问题描述: Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longes...

fanyuwgy的博客 146

[leetcode 32]Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()",

ER_Plough的专栏 763

LeetCode //C - 1223. Dice Roll Simulation

Summary: This problem involves counting the number of distinct sequences possible when rolling a die n times, with constraints on the maximum consecutive occurrences of each number. The solution uses dynamic programming to track sequences ending with each

Made in Code 206

ThinkpadT61 2.29BIOS白名单SATAⅡSLIC2.1换CPU温度报警问题

ThinkpadT61白名单SATAⅡSLIC2.1换CPU温度报警问题。全套刷机工具,WINPE32、免电池刷机补丁。全网最全。

二阶倒立摆,二阶倒立摆的simulink仿真,matlab源码.rar

二阶倒立摆,二阶倒立摆的simulink仿真,matlab源码.rar

上一篇: [LeetCode]31. Next Permutation
下一篇: [LeetCode]104. Maximum Depth of Binary Tree
charon____
博客等级 码龄11年 3粉丝 202原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值