stack-and-queue/stack/longest-valid-parentheses
优质
小牛编辑
123浏览
2023-12-01
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.
分析
无
使用栈
# Longest Valid Parenthese
# Using a stack
# Time Complexity: O(n), Space Complexity: O(n)
class Solution:
def longestValidParentheses(self, s: str) -> int:
max_len = 0
stack = deque()
stack.append(-1)
for i in range(len(s)):
if s[i] == '(':
stack.append(i);
else:
stack.pop()
if len(stack) == 0:
stack.append(i)
else:
max_len = max(max_len, i - stack[-1])
return max_len
// Longest Valid Parenthese
// Using a stack
// Time Complexity: O(n), Space Complexity: O(n)
public class Solution {
public int longestValidParentheses(String s) {
int maxLen = 0;
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) =='(') {
stack.push(i);
} else {
stack.pop();
if(stack.empty()) {
stack.push(i);
} else {
maxLen = Math.max(maxLen, i - stack.peek());
}
}
}
return maxLen;
}
}
// Longest Valid Parenthese
// Using a stack
// Time Complexity: O(n), Space Complexity: O(n)
class Solution {
public:
int longestValidParentheses(string s) {
int max_len = 0;
stack<int> stack;
stack.push(-1);
for (int i = 0; i < s.length(); ++i) {
if (s[i] == '(') {
stack.push(i);
} else {
stack.pop();
if(stack.empty()) {
stack.push(i);
} else {
max_len = max(max_len, i - stack.top());
}
}
}
return max_len;
}
};
Dynamic Programming, One Pass
// Longest Valid Parenthese
// 时间复杂度O(n),空间复杂度O(n)
// @author 一只杰森(http://weibo.com/wjson)
class Solution {
public int longestValidParentheses(final String s) {
int[] f = new int[s.length()];
int result = 0;
for (int i = s.length() - 2; i >= 0; --i) {
int match = i + f[i + 1] + 1;
// case: "((...))"
if (s.charAt(i) == '(' && match < s.length() &&
s.charAt(match) == ')') {
f[i] = f[i + 1] + 2;
// if a valid sequence exist afterwards "((...))()"
if (match + 1 < s.length()) f[i] += f[match + 1];
}
result = Math.max(result, f[i]);
}
return result;
}
}
// Longest Valid Parenthese
// 时间复杂度O(n),空间复杂度O(n)
// @author 一只杰森(http://weibo.com/wjson)
class Solution {
public:
int longestValidParentheses(const string& s) {
vector<int> f(s.size(), 0);
int result = 0;
for (int i = s.size() - 2; i >= 0; --i) {
int match = i + f[i + 1] + 1;
// case: "((...))"
if (s[i] == '(' && match < s.size() && s[match] == ')') {
f[i] = f[i + 1] + 2;
// if a valid sequence exist afterwards "((...))()"
if (match + 1 < s.size()) f[i] += f[match + 1];
}
result = max(result, f[i]);
}
return result;
}
};
两遍扫描
// Longest Valid Parenthese
// 两遍扫描,时间复杂度O(n),空间复杂度O(1)
// @author 曹鹏(http://weibo.com/cpcs)
class Solution {
public int longestValidParentheses(final String s) {
int result = 0, depth = 0, start = -1;
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) == '(') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = Math.max(result, i - start);
}
}
}
depth = 0;
start = s.length();
for (int i = s.length() - 1; i >= 0; --i) {
if (s.charAt(i) == ')') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = Math.max(result, start - i);
}
}
}
return result;
}
}
// Longest Valid Parenthese
// 两遍扫描,时间复杂度O(n),空间复杂度O(1)
// @author 曹鹏(http://weibo.com/cpcs)
class Solution {
public:
int longestValidParentheses(const string& s) {
int result = 0, depth = 0, start = -1;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = max(result, i - start);
}
}
}
depth = 0;
start = s.size();
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] == ')') {
++depth;
} else {
--depth;
if (depth < 0) {
start = i;
depth = 0;
} else if (depth == 0) {
result = max(result, start - i);
}
}
}
return result;
}
};