题解 | 有效括号序列
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
#include <cstring>
#include <stack>
class Solution {
public:
stack<char> C_stack;
bool isValid(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
C_stack.push(s[i]);
} else {
if(!C_stack.empty()){
char temp_char=C_stack.top();
C_stack.pop();
if(s[i]==')'){
if(temp_char!='(')
return false;
}
if(s[i]==']'){
if(temp_char!='[')
return false;
}
if(s[i]=='}'){
if(temp_char!='{')
return false;
}
}else {
return false;
}
}
}
if(C_stack.empty()){
return true;
}else {
return false;
}
}
} ;
神州信息成长空间 29人发布