Leetcode 20 Valid Parentheses 判断括号匹配
2018-03-21 18:15
531 查看
题目描述:
Given a string containing just the characters
给出一系列括号,判断匹配与否,注意这里有小括号,中括号,大括号
思路:
1.典型的需要用到栈的括号匹配问题,每次遇到左括号( [ {等,都将之入栈;每次遇到右括号) ] }等,都从栈中弹出相对应的左括号,如果没有对应的或者栈空,则不匹配
2.时间复杂度O(n),空间复杂度O(n)
源代码:class Solution {
public:
bool isValid(string s)
{
int i=0;
if(s.size()<=1) return 0;
stack<char> st;char temp,top;
while(i<s.size())
{
if(st.empty()) {st.push(s[i]);i++;}
temp = (char)s[i];top=st.top();
switch(temp)
{
case')':if(top == '(') {st.pop();} else return 0;break;
case']':if(top == '[') {st.pop();} else return 0;break;
case'}':if(top == '{') {st.pop();} else return 0;break;
case'(':st.push('(');break;
case'[':st.push('[');break;
case'{':st.push('{');break;
default: return 0;
}
i++;
}
if(st.empty()) return 1; else return 0;
}
};
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.
给出一系列括号,判断匹配与否,注意这里有小括号,中括号,大括号
思路:
1.典型的需要用到栈的括号匹配问题,每次遇到左括号( [ {等,都将之入栈;每次遇到右括号) ] }等,都从栈中弹出相对应的左括号,如果没有对应的或者栈空,则不匹配
2.时间复杂度O(n),空间复杂度O(n)
源代码:class Solution {
public:
bool isValid(string s)
{
int i=0;
if(s.size()<=1) return 0;
stack<char> st;char temp,top;
while(i<s.size())
{
if(st.empty()) {st.push(s[i]);i++;}
temp = (char)s[i];top=st.top();
switch(temp)
{
case')':if(top == '(') {st.pop();} else return 0;break;
case']':if(top == '[') {st.pop();} else return 0;break;
case'}':if(top == '{') {st.pop();} else return 0;break;
case'(':st.push('(');break;
case'[':st.push('[');break;
case'{':st.push('{');break;
default: return 0;
}
i++;
}
if(st.empty()) return 1; else return 0;
}
};
相关文章推荐
- LeetCode 20 Valid Parentheses(用栈判断括号匹配)
- LeetCode20——Valid Parentheses(括号匹配问题,使用栈的知识)
- LeetCode 20 Valid Parentheses(括号匹配)
- leetcode 20 Valid Parentheses 括号匹配
- leetcode 20 Valid Parentheses 括号匹配
- leetcode 20 Valid Parentheses 括号匹配
- Leetcode刷题记——20. Valid Parentheses (有效的括号即括号匹配)
- LeetCode 20 Valid Parentheses 括号匹配问题
- LeetCode-20 Valid Parentheses(判断括号是否规范)
- LeetCode 20 Valid Parentheses (括号匹配问题)
- LeetCode Valid Parentheses(判断括号是否匹配)
- LeetCodet题解--20. Valid Parentheses(括号匹配问题)
- LeetCode 20. Valid Parentheses(括号校验)
- LeetCode | Longest Valid Parentheses(最长有效的括号匹配)
- LeetCode | Valid Parentheses(括号匹配)
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
- Longest Valid Parentheses(最长的括号匹配)【面试算法leetcode】
- LeetCode 32 Longest Valid Parentheses 最大合法括号匹配长度计算 动态规划算法有待学习
- 20 Valid Parentheses(匹配括号)
- LeetCode 20 Valid Parentheses(有效的括号)