32. Longest Valid Parentheses (Stack; DP)
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.
法I:把所有invalid的括号位置都标记出来,比较invalid之间的长度哪段最长
- class Solution {
- public:
- int longestValidParentheses(string s) {
- vector<int> invalidPos;
- invalidPos.push_back(-);
- invalidPos.push_back(s.length());
- stack<int> lParenPos;
- int len = , ret = ;
- for(int i = ; i < s.length(); i++){
- if(s[i]=='('){
- lParenPos.push(i);
- }
- else{ //right parenthese
- if(lParenPos.empty()){
- invalidPos.push_back(i);
- }
- else{
- lParenPos.pop();
- }
- }
- }
- while(!lParenPos.empty()){
- invalidPos.push_back(lParenPos.top());
- lParenPos.pop();
- }
- sort(invalidPos.begin(), invalidPos.end());
- for(int i = ; i < invalidPos.size(); i++){
- len = invalidPos[i]-invalidPos[i-]-;
- if(len > ret) ret = len;
- }
- return ret;
- }
- };
法II:动态规划
- class Solution {
- public:
- int longestValidParentheses(string s) {
- if(s.empty()) return ;
- stack<int> leftStack;
- int ret = ;
- int currentMax = ;
- int leftPos;
- vector<int> dp(s.length()+,); //currentMax无法检测到连续valid的情况,eg: ()(), 所以需要动态规划记录i位置之前连续多少个valid。
- for(int i = ; i <s.length(); i++){
- if(s[i]==')'){
- if(leftStack.empty()){
- currentMax = ;
- }
- else
- {
- leftPos = leftStack.top();
- leftStack.pop();
- currentMax = i-leftPos+ + dp[leftPos];
- dp[i+] = currentMax;
- ret = max(ret,currentMax);
- }
- }
- else{
- leftStack.push(i); //push the index of '('
- }
- }
- return ret;
- }
- };
32. Longest Valid Parentheses (Stack; DP)的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 表格头部与左侧内容随滚动条位置改变而改变(基于jQuery)
效果图如下: HTML代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta chars ...
- Python 函数 memoryview()
memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...
- bzoj 4407 于神之怒加强版——反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4407 \( ans = \sum\limits_{D=1}^{min(n,m)}\frac{ ...
- bzoj1040(ZJOI2008)骑士——基环树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1040 基环树的模板. 套路就是把环断开,先把一端作为根节点,强制不选:再把另一端作为根节点, ...
- MongoDB入门实践
MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...
- Unit07: MyBatis框架简介 、 MyBatis基本应用
Unit07: MyBatis框架简介 . MyBatis基本应用 1. myBatis (1)myBatis是什么? 是一个开源的持久层框架. 注:myBatis底层仍然是jdbc. (2)编程步骤 ...
- Python Socke
回射 SERVER #!/usr/bin/python3 #_*_ coding:utf- _*_ import socket,os,time import socketserver import t ...
- 贴一段demo代码,演示channel之间的同步
package main import ( "fmt" "time" ) func deskGoRoutine(index int, userChannel c ...
- GOF23设计模式之观察者模式(observer)
一.观察者模式概述 观察者模式主要用于 1 :N 的通知.当一个对象(目标对象 Subject 或 Observable)的状态变化时,它需要通知一系列对象(观察者对象 Observer),令它们做出 ...
- 在Windows下搭建基于nginx的视频直播和点播系统
http://my.oschina.net/gaga/blog/478480 一.软件准备 由于nginx原生是为linux服务的,因此官方并没有编译好的windows版本可以下载,要在windows ...