leetcode记录32.Longest Valid Parentheses
hard
Runtime: 8 ms, faster than 98.79% of C++ online submissions for Longest Valid Parentheses.
Memory Usage: 10.6 MB, less than 80.80% of C++ online submissions for Longest Valid Parentheses.
class Solution {
public:
int longestValidParentheses(string s) {
if (s.size() <= 1)
{
return 0;
}
vector<int> dp(s.size());
stack<char> st;
stack<int> idx;
int max = 0;
st.push(s[0]);
idx.push(0);
for (int i = 1; i < s.size(); i++)
{
if (!st.empty() && st.top() == '(' && s[i] == ')')
{
dp[idx.top()] = 1;
dp[i] = 1;
st.pop();
idx.pop();
}
else
{
st.push(s[i]);
idx.push(i);
}
}
int temp = 0;
for (int i = 0; i < s.size(); i++)
{
if (dp[i] == 0)
{
temp = 0;
}
else
{
temp += dp[i];
}
if (temp > max)
{
max = temp;
}
}
return max;
}
};