【leetcode】Longest Valid Parentheses

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.

设dp[i]表示从s[i]开始(包括s[i]),到达s[n-1]时,最长的有效括号对的长度。

如果s[i]==‘(‘

我们需要检查j=dp[i+1]+i+1对应的s[j]位置的括号是否为")",如果s[j]为‘)‘则说明又组成了一对括号

故此时dp[i]=dp[i+1]+2

于此同时,我们还需要继续考虑dp[j+1]的值,如果j+1没有超出范围,则dp[i]=dp[i+1]+2+dp[j+1]

其他情况dp[i]=0;

 1 class Solution {
 2 public:
 3     int longestValidParentheses(string s) {
 4         int n=s.length();
 5         if(n==0) return 0;
 6
 7         int *dp=new int[n];
 8         dp[n-1]=0;
 9
10         int result=0;
11         for(int i=n-2;i>=0;i--)
12         {
13             if(s[i]==‘(‘)
14             {
15                 int j=dp[i+1]+i+1;
16
17                 if(s[j]==‘)‘)
18                 {
19                     dp[i]=dp[i+1]+2;
20                     if(j<n-1) dp[i]+=dp[j+1];
21                 }
22                 else
23                 {
24                     dp[i]=0;
25                 }
26
27                 if(dp[i]>result)
28                 {
29                     result=dp[i];
30                 }
31             }
32             else
33             {
34                 dp[i]=0;
35             }
36         }
37         delete [] dp;
38         return result;
39     }
40 };
时间: 2024-12-29 06:56:59

【leetcode】Longest Valid Parentheses的相关文章

【LeetCode】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

【栈】Longest Valid Parentheses

题目:leetcode 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 "()", whic

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha

【leetcode刷题笔记】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 &

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

leetcode 之 Longest Valid Parentheses

leetcode中和括号匹配相关的问题共有三个,分别是: Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are a

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字