描述
Given a string containing just the characters ’(’, ’)’, ’{’, ’}’, ’[’ and ’]’, determine if the
input string is valid.
e brackets must close in the correct order, ”()” and ”()[]” are all valid but ”(]” and ”([)]” are
not.
class Solution{
public:
bool isValid(string const &s){
string left = "([{";
string right = ")]}";
stack<char> stk;
for(auto c : s){
if(left.find(c) != string::npos){
stk.push(c);
}else{
if(stk.empty() || stk.top() != left[right.find(c)])
return false;
else
stk.pop();
}
}
return stk.empty();
}
};
/**********************************************************************************************************
Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed)
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.
**********************************************************************************************************/
class Solution{
public:
int longestValidParentheses(string s){
int res = 0, start = 0, n = s.size();
stack<int > st;
for(int i = 0;i < n;i++){
if(s[i] = '(')
st.push(i);
else if(s[i] == ')'){
if(st.empty()){
start = i + 1;
}else{
st.pop();
res = st.empty() ? max(res , i - start + 1) : max(res, i - st.top());
}
}
}
return res;
}
};
参考资料:
LeetCode题解