【leetcode】20. Valid Parentheses
2016-05-24 12:40
337 查看
/** * 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. */ #include <iostream> #include <string> #include <vector> #include <stack> using namespace std; bool isValid(string s) { stack<char> char_stack; for (int i = 0; i < s.length(); i++) { if (s[i] == '(' || s[i] == '{' || s[i] == '[') { char_stack.push(s[i]); } else if (s[i] == ')' || s[i] == '}' || s[i] == ']') { if (s[i] == ')' && char_stack.top() == '(') { char_stack.pop(); } else if (s[i] == '}' && char_stack.top() == '{') { char_stack.pop(); } else if (s[i] == ']' && char_stack.top() == '[') { char_stack.pop(); } else { return false; } } else continue; } return char_stack.empty(); } int main() { string str = "([)]"; cout << isValid(str); system("pause"); return 0; }
相关文章推荐
- [mac]添加系统自带辞典或下载的词典包
- ASP.NET——初步了解
- 定制班第九课 Receiver在Driver的精妙实现全生命周期彻
- Davinci DM6446开发攻略-UBOOT-2009.03移植2 nand flash的烧写
- 凌乱
- Redis常用命令
- ath10 移植记录
- iOS 苹果官方Demo合集
- 将string转为同名类名,方法名。(c#反射)
- 《善圈第27期天天圈》5.23精彩回顾:善圈微商,干货鸡汤!
- ubuntu14.04安装python3
- 删除文件中的 ^M 字符
- java并发编程线程安全问题
- HDU 2066 一个人的旅行(Dijkstra求特殊点的单源最短路径)
- 一起来学英语
- NSArray数组排列
- input分析
- 选择排序算法
- jquery简单插件制作(fn.extend)完整实例
- 引入佈局