<LeetCode OJ> 20. Valid Parentheses
2017-06-16 21:27
411 查看
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.
我的解法:时间复杂度O(N)
一旦出现左型括号,先存储起来就准备与右型括号配对,假设是右型括号,就运行配对(检查存储之中是否有左型括号)!
从标签来看,利用栈来完毕如此操作,左型括号先压栈等待匹配,
右型括号就查看栈顶是否是左型括号(假设不是已经不匹配了。"([)]" are not.)
最后通过栈是否是空的来推断是否是全然配对
学习别人的算法:
运用数据结构红黑树map来存储配对的括号,以便查找匹配情况(时间浮渣度N*lg(N))
一旦出现左型括号,就存储相应的右型括号,
假设是右型括号,就运行配对(检查存储之中是否有左型括号)!
假设匹配则删除vector的最后一个元素
最后通过vc是否是空的来推断是否是全然配对
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50380804
原作者博客:http://blog.csdn.net/ebowtang
'(',
')',
'{',
'}',
'['and
']',
determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are
all valid but
"(]"and
"([)]"are
not.
我的解法:时间复杂度O(N)
一旦出现左型括号,先存储起来就准备与右型括号配对,假设是右型括号,就运行配对(检查存储之中是否有左型括号)!
从标签来看,利用栈来完毕如此操作,左型括号先压栈等待匹配,
右型括号就查看栈顶是否是左型括号(假设不是已经不匹配了。"([)]" are not.)
最后通过栈是否是空的来推断是否是全然配对
class Solution { public: bool isValid(string str) { stack<char> s; if(str.size() <= 1) return false; for(int i=0;i<str.size();i++) { switch(str[i]) { //左型括号直接压栈 case '(' : s.push('('); break; case '{' : s.push('{'); break; case '[' : s.push('['); break; //右型括号运行匹配操作,能匹配则弹出栈顶,不能则返回false case ')' : if(s.empty()) { return false; }else{ char ch=s.top(); if(ch=='(') { s.pop(); continue; }else{ return false; break; } } break; case '}' : if(s.empty()) { return false; }else{ char ch=s.top(); if(ch=='{') { s.pop(); continue; }else{ return false; break; } } break; case ']' : if(s.empty()) { return false; }else{ char ch=s.top(); if(ch=='[') { s.pop(); continue; }else{ return false; break; } } break; } } //最后若是空则完毕匹配 if(s.empty()) return true; else return false; } };
学习别人的算法:
运用数据结构红黑树map来存储配对的括号,以便查找匹配情况(时间浮渣度N*lg(N))
一旦出现左型括号,就存储相应的右型括号,
假设是右型括号,就运行配对(检查存储之中是否有左型括号)!
假设匹配则删除vector的最后一个元素
最后通过vc是否是空的来推断是否是全然配对
class Solution { public: bool isValid(string s) { vector<char> vc; map<char, char> mapping; mapping['('] = ')'; mapping['{'] = '}'; mapping['['] = ']'; for (int i = 0; i < s.length(); ++i) { if (s[i] == '(' || s[i] == '{' || s[i] == '[') //假设是左型括号。就直接压入相应的右型括号到vector vc.push_back(mapping[s[i]]); else if (vc.empty() || vc.back() != s[i])//假设右型括号的不匹配情况 return false; else //右型括号的匹配情况 vc.pop_back();//删除vector中的最后一个元素 } return vc.empty() ? true : false; } };
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50380804
原作者博客:http://blog.csdn.net/ebowtang
相关文章推荐
- <LeetCode OJ>Valid Parentheses【20】
- <LeetCode OJ> 241. Different Ways to Add Parentheses
- leetcode 第20题<Valid Parentheses>(java)
- 【leetcode】20. Valid Parentheses(Python & C++)
- leetcode题解-13. Roman to Integer && 14. Longest Common Prefix && 20. Valid Parentheses
- Leetcode_20_Valid Parentheses
- LeetCode 20 — Valid Parentheses(C++ Java Python)
- LeetCode --- 20. Valid Parentheses
- 20. Valid Parentheses Leetcode Python
- leetcode-20 Valid Parentheses
- LeetCode20——Valid Parentheses
- LeetCode20: Valid Parentheses
- LeetCode 20.Valid Parentheses
- [LeetCode] 20.Valid Parentheses
- [Leetcode 20, Easy] Valid Parentheses
- 20.Valid Parentheses&合法括号序列判断
- 【LeetCode】20. Valid Parentheses
- leetcode 第20-21题 Valid Parentheses & Generate Parentheses
- [LeetCode]20 Valid Parentheses
- leetcode20-Valid Parentheses