【Python】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;
}
};
【Python】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
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
随机推荐
- Visual Studio 当前不会命中断点的问题
这个问题一般有两个版本 1.当前不会命中断点,还没有为该文档加载任何符号. 2.当前不会命中断点,源代码与原始版本不同. 要解决第一个问题,就要了解一种文件格式“PDB(Program DataBas ...
- Effective C++(19) 设计class犹如设计type
问题聚焦: 这一节不涉及代码,但是我们需要明确的一点是,思想比代码要重要得多. 设计优秀的classes是一项艰巨的工作,就像设计好的types一样. 我们应该带着和“语言设计 ...
- 网际协议:无连接数据报交付(IPv4)
一.数据报格式:
- Android编程心得-在任意类中获取当前屏幕宽高
进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...
- C#使用文件监控对象FileSystemWatcher 实现数据同步
在C#使用文件监控对象FileSystemWatcher 实现数据同步 2013-12-12 18:24 by 幕三少, 352 阅读, 3 评论, 收藏, 编辑 最近在项目中有这么个需求,就是得去实 ...
- Day4:T1小技巧(类似于指针操作)T2搜索+小细节
Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...
- MVC 5显示、创建、编辑、删除等功能实练
MVC 5显示.创建.编辑.删除等功能实练 在前天的学习小结中<15天学习MVC后的小结(分享经历与想法)>http://www.cnblogs.com/insus/p/3369870.h ...
- js中内置有对象
statpot:使用mongo+bootstrap+highcharts做统计报表 最近做了一个统计项目,这个统计项目大致的需求是统计接口的访问速度.客户端会调用一个接口来记录接口的访问情况,我的需求 ...
- .NET核心代码保护策略
.NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...
- Asp.net QueryString批量插入和更新
public static string InsertOrUpdateQueryString(string[] keys, string[] values) { return InsertOrUpda ...