知识点:C++ for(char c:s) 相当于JAVA的增强for循环的语法结构。相当于C++的:
for( int i = 0; i < s.length(); i++)
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.
solution:
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(char c : s){
if(c == '('|| c == '{' || c == '['){
st.push(c);
}else{
if(st.empty()) return false;
if(c == ')' && st.top() != '(') return false;
if(c == '}' && st.top() != '{') return false;
if(c == ']' && st.top() != '[') return false;
st.pop();
}
}
return st.empty();
}
};
本文介绍了一种使用C++实现的有效括号匹配算法。该算法利用栈来判断字符串中的括号是否正确配对,包括圆括号'()'、方括号'[]'和花括号'{}

430

被折叠的 条评论
为什么被折叠?



