[leetcode] Valid Parentheses
来源:互联网 发布:淘宝客服能在家里做吗 编辑:程序博客网 时间:2024/06/15 07:36
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.
This is a classical case for the usage of stack.
1. when '(', '{' or '[', push into the stack.
2. otherwise, if the stack is empty, then occurs mismatching, return false. or, if the element in the top of the stack mismatch with s[i], then return false. otherwise pop the stack.
3. finally we should check if the stack is empty, if not, return false.
runtime:
source code:
bool isValid(string s) {stack<char> chstack;for(int i=0; i<s.size(); i++) {if(s[i] == '(' || s[i] == '[' || s[i] == '{') {chstack.push(s[i]);} else {if(chstack.empty()) { return false; }switch(s[i]) {case ']': if(chstack.top() != '[') {return false;} break;case '}':if(chstack.top() != '{') {return false;} break;case ')':if(chstack.top() != '(') {return false;} break;default :break;}chstack.pop();}}if( !chstack.empty() ) return false;else return true;}
0 0
- LeetCode: Valid Parentheses
- LeetCode: Longest Valid Parentheses
- LeetCode: Valid Parentheses
- LeetCode Longest Valid Parentheses
- [Leetcode] Valid Parentheses
- LeetCode: Longest Valid Parentheses
- [Leetcode] Longest Valid Parentheses
- LeetCode : Valid Parentheses
- [leetcode]longest Valid Parentheses(!!)
- Leetcode: Valid Parentheses
- leetcode 52: valid parentheses
- [Leetcode] Longest Valid Parentheses
- leetcode Longest Valid Parentheses
- 【leetcode】Valid Parentheses
- 【leetcode】Longest Valid Parentheses
- [LeetCode]Longest Valid Parentheses
- [LeetCode]Valid Parentheses
- LeetCode-Valid Parentheses
- centos mysql安装
- Lexicographically minimum string rotation
- JavaScript系列二:变量、数组、流程控制、函数以及事件响应
- 1501171340-蓝桥杯-算法提高 逆序排列
- microsoftoffice2013 破解方法(含有破解工具)
- [leetcode] Valid Parentheses
- Spark的核心概念
- (KVM连载)3.4 编译qemu-kvm和安装qemu-kvm
- 好友列表
- KAFKA分布式消息系统
- windows 消息类型 大全
- 牛顿法求解非线性方程
- Spark架构图
- 日赚两百兼职,淘宝信誉销量提升!