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
all valid but "(]"
and "([)]"
are
not.
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]表示到第i個位置的最大長度,由於匹配的括號必須是連續的,所以,如果有j < i 且j和i匹配,則dp[i] = (i-j+1)+dp[j]。
從轉移方程來看,好像是二維DP,但是可以使用堆疊來轉化為一維的,簡單來說,
就是遇到左括號就進棧,遇到右括號就出棧,而出棧的位置就是上面的j,所以不需
要進行二維掃描就可定位到j。
class Solution {
public:
int longestValidParentheses(string s) {
int length = s.size(),i,maxLength = 0;
vector<int> dp(length,0);
stack<int> stk; // 左括號的下標
for(i = 0; i < length;++i)
{
if(s[i] == '(')stk.push(i);
else
{
if(!stk.empty())
{
int start = stk.top();
stk.pop();
dp[i] = i - start + 1;
if(start > 0)dp[i] += dp[start-1];
if(dp[i] > maxLength)maxLength = dp[i];
}
}
}
return maxLength;
}
};
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
相關文章
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode-Longest Valid ParenthesesLeetCode
- Longest Valid Parentheses leetcode javaLeetCodeJava
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- leetcode32_Longest Valid ParenthesesLeetCode
- [LeetCode] Longest Valid Parentheses 最長有效括號LeetCode
- Leetcode Valid ParenthesesLeetCode
- Leetcode 20 Valid ParenthesesLeetCode
- Leetcode-Valid ParenthesesLeetCode
- Valid Parentheses leetcode javaLeetCodeJava
- 【leetcode】32. Longest Valid Parentheses 最長的有效匹配括號子串長度LeetCode
- [LeetCode] Valid Parentheses 驗證括號LeetCode
- LeetCode 之 JavaScript 解答第20題 —— 有效的括號(Valid Parentheses)LeetCodeJavaScript
- LeetCode Valid Parentheses(020)解法總結LeetCode
- Leetcode 20 有效的括號valid-parentheses(棧)LeetCode
- 演算法Valid Parentheses演算法
- Leetcode 22 Generate ParenthesesLeetCode
- Leetcode-Generate ParenthesesLeetCode
- Generate Parentheses leetcode javaLeetCodeJava
- leetcode22_Generate ParenthesesLeetCode
- LeetCode-Remove Invalid ParenthesesLeetCodeREM
- [LeetCode] Generate Parentheses 生成括號LeetCode
- Leetcode Valid NumberLeetCode
- Leetcode Valid AnagramLeetCode
- Leetcode Valid SudokuLeetCode
- Leetcode Longest Common PrefixLeetCode
- [LeetCode]Longest Common PrefixLeetCode
- Leetcode - 022. Generate ParenthesesLeetCode
- LeetCode-Different Ways to Add ParenthesesLeetCode
- Leetcode 36 Valid SudokuLeetCode
- Leetcode Valid PalindromeLeetCode
- Leetcode-Valid SudokuLeetCode
- Leetcode-Valid NumberLeetCode
- Valid Sudoku leetcode javaLeetCodeJava
- Valid Number leetcode javaLeetCodeJava
- Leetcode 14 Longest Common PrefixLeetCode
- LeetCode- Longest Absolute File PathLeetCode
- LeetCode-Longest Increasing SubsequenceLeetCode