leetcode32】的更多相关文章

题目: 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…
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 longest valid parentheses substring is "()" Example…
class Solution { public: int longestValidParentheses(string s) { ; stack<int> st; ; 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()) lon…
32. 最长有效括号 做法 \(f_{i}\)以\(i\)结尾的最长匹配 前提为\(s[i]=')'\) \(s[i-1]='('\),则\(f[i]=f[i-2]+2\) \(s[i-1]=')'\And s[i-1-f[i-1]]='('\),则\(f[i]=f[i-1]+f_[i-2-f[i-1]]+2\)…
有一说一,我觉得这题没有到困难级 要保存之前的状态,感觉是很明显的dp 思路和题解一样 class Solution { public: int longestValidParentheses(string s) { int len=s.length(); int ret = 0; int *dp=new int[len]; for(int i=0;i<len;i++) dp[i]=0; for (int i = 1; i < s.length(); i++) { if (s[i] == ')…