【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 "()"
, which has length = 2.
Another example is ")()())"
, where the longest valid parentheses substring is "()()"
, which has length = 4.
题意:求最长的合法括号长度
思路1,用栈:1.遍历字符串,当遇到'('的时候,把其索引放入堆栈,当遇到')'时候,如果栈顶是'(',则出栈操作一次,否则索引入栈
2.当栈为空时候,说明字符串全体合法,返回字符串长度
3.如果栈不为空,则比较栈中相邻索引的“空洞”长度,最长的空洞即为所求
class Solution {
public:
int longestValidParentheses(string s) {
int n=s.length(),i,longest=;
stack<int> st;
for(i=;i<n;i++){
if('('==s[i])
st.push(i);
else{
if(!st.empty()){
if(s[st.top()]=='(')
st.pop();
else
st.push(i);
}
else{
st.push(i);
}
}
}
if(st.empty()) longest = n;
else{
int rear=n,front=;
while(!st.empty()){
front=st.top();
st.pop();
longest = max(longest,rear--front);
rear = front;
}
longest = max(longest,front);
}
return longest;
}
};
思路2:动态规划
用longest[]记录字符串中截至每个位置时的最长合法字符串长度
如果s[i]为'(',那么longest[i]为0,因为左右以'('结尾的字符串肯定不合法,为0
如果s[i]为')',那么分两种情况
1.当s[i-1]为'('时候, longest[i] = longest[i-2] + 2
2.当s[i-1]为')',和s[i-longest[i-1]-1] == '('时,longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2];
longest[i-longest[i-1]-2]代表上一个以')'结尾的合法字符串
class Solution {
public:
int longestValidParentheses(string s) {
int len=s.length();
if(len<=)
return ;
int curmax=;
vector<int> longest(len,);
for(int i=;i<len;i++){
if(')'==s[i]){
if('('==s[i-]){
longest[i]=(i->?longest[i-]+:);
curmax=max(longest[i],curmax);
}
else{
if(i-longest[i-]->=&&s[i-longest[i-]-]=='('){
longest[i]=longest[i-]++(i-longest[i-]->?longest[i-longest[i-]-]:);
curmax=max(longest[i],curmax);
}
}
}
}
return curmax;
}
};
【LeetCode】32. Longest Valid Parentheses的更多相关文章
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- 【Python】32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- HDU 5213 分块 容斥
给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...
- [Luogu 2024] 食物链
[Luogu 2024] 食物链 几句随感 我依稀记得联赛前本来想做这题的时候. 当年啊弱到题目与标签就令我望而生畏. 还有翻阅很多遍那现在已经被遗弃的博客. 看到题解中「三倍数组」的字眼就怕难而放弃 ...
- Crash Consistency : FSCK and Journaling
现在开始今天的第三篇博客的撰写,不能扯淡了,好多任务啊.但是还是忍不住吐槽一下,之前选择这篇文章纯属是个意外,我把Crash看做了Cache,唉,要不然也就不用写这篇文章了. 1. 这篇博客讲什么? ...
- HashSet的特性介绍
HashSet除了在元素的存储上是无序的以外,还是不能够存储重复的元素. HashSet如何判断元素是否重复呢?是根据元素继承的两个方法来判断,hashCode和equals,当存储元素时,首先判断要 ...
- ORACLE ASM中查询表空间使用情况、数据文件路径、裸设备磁盘总大小剩余大小
在ASM中:查询所有磁盘名称.总大小.剩余大小:单位MB-----查看组的信息(总大小)select name,total_mb, free_mb from v$asm_diskgroup; ---查 ...
- nginx配置不当导致的目录遍历下载漏洞-“百度杯”CTF比赛 2017 二月场
题目:http://98fe42cede6c4f1c9ec3f55c0f542d06b680d580b5bf41d4.game.ichunqiu.com/login.php 题目内容: 网站要上线了, ...
- Python标准库笔记(5) — sched模块
事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...
- photoshop 安装问题
问题:“安装程序检测到计算机重新启动操作可能处于挂起状态.建议您退出安装程序,重新启动并重试.” 解决: 1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE ...
- 142.Linked List Cycle II---双指针
题目链接 题目大意:141题目的扩展,给出单链表,判断是否有环,如果有环,找出环的开始的结点,如果没有环,返回null. 法一(借鉴):在已经找出单链表环的基础上再找开始结点,要时刻记住这个环不一定是 ...
- 用于启动 Windows Phone 8 内置应用的 URI 方案
本主题列出了可用于启动内置应用的 URI 方案.许多内置于 Windows Phone 的应用,都可以通过调用 LaunchUriAsync(Uri) 和传入一个使用与要启动应用相关的方案的 URI, ...