Valid Parentheses
来源:互联网 发布:gta5捏脸数据日本妹子 编辑:程序博客网 时间:2024/04/30 06:00
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.
此题难度不大,是基本的思路。右边字符是要匹配消掉的。左边是存入的!所以重点在右边字符!
bool isValid(string s) { int i = 0; int index = 0; string right_str = ")}]"; string left_str = "({["; stack<char> my_stack; while(s[i] != '\0') { if((index=right_str.find(s[i])) >= 0) { if(my_stack.empty()) { return false; } else { //另一种非索引方式 //if((my_stack.top() == '('&&s[i]==')')||(my_stack.top() == '{'&&s[i]=='}')||(my_stack.top() == '['&&s[i]==']')) if(my_stack.top() == left_str[index]) my_stack.pop(); else return false; } } else { my_stack.push(s[i]); } i++; } return my_stack.empty(); }
0 0
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- Valid Parentheses
- A. K-Periodic Array
- 主成成分分析pca算法 原理解析
- 再谈降维算法--PCA算法
- 遗传算法解决TSP问题实现以及与最小生成树的对比
- Ubuntu 12.04下安装VMware Tools
- Valid Parentheses
- 怎么让div始终沉于底部
- 汇编程序的二维数组的访问
- html中radio值的获取、赋值、注册事件。
- 做一个产品而去创业,和为了创业而去做一个产品是不一样的
- 嵌入式 脚本实现hi3518c程序升级示例经典if,then,else,exit,以及字符串比较和经典常识
- 压缩及解压缩
- oracle 11g 安装以及注意事项
- WM_IME_CHAR使用