20. Valid Parentheses
2017-01-05 09:15
225 查看
Given a string containing just the characters
determine if the input string is valid.
The brackets must close in the correct order,
all valid but
not.
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();
}
};
'(',
')',
'{',
'}',
'['and
']',
determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are
all valid but
"(]"and
"([)]"are
not.
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();
}
};
相关文章推荐
- FTPrep, 20 Valid Parentheses
- 20. Valid Parentheses
- leetcode20:Valid Parentheses
- 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses LeetCode
- 20. Valid Parentheses
- LeetCode 20. Valid Parentheses
- Leetcode 20. Valid Parentheses
- 【LeetCode OJ 20】Valid Parentheses
- Leetcode 20. Valid Parentheses
- 20. Valid Parentheses
- 24.leetcode题目20: Valid Parentheses
- leetcode——20——Valid Parentheses
- [LeetCode] 20. Valid Parentheses
- leetcode-20-Valid Parentheses
- LeetCode-20 Valid Parentheses(判断括号是否规范)
- [C++]LeetCode 20: Valid Parentheses(判断运算符有效性)
- LeetCode 20 Valid Parentheses (括号匹配问题)
- LeetCode 20 Valid Parentheses (C,C++,Java,Python)