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、遍历字符串:

  (1)符号的左半边,则入栈;

  (2)符号的右半边,则pop出栈顶元素,如果栈为空,则返回false;

     判断此右半边和栈顶元素是否匹配;匹配继续;不匹配则返回false;

3、循环结束后栈为空,则true,不为空则false;

 class Solution {
public:
bool isValid(string s) {
stack<char> st;
int len = s.length(); for(int i = ; i < len; ++i) {
if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
if (st.empty())
return false;
char c = st.top();
if ((c == '(' && s[i] != ')') || \
(c == '[' && s[i] != ']') || \
(c == '{' && s[i] != '}'))
return false;
st.pop(); } else {
st.push(s[i]);
}
} return st.empty();
}
};

附录:

常见符号ASCII码

C++常见容器使用及公式

最新文章

  1. 版本管理工具SVN
  2. Chrome浏览器Network面板http请求时间分析
  3. SO单号中某两项没进FP
  4. 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证
  5. [Angularjs]ng-switch用法
  6. ctDNA 相关网站-liquid-biopsy
  7. HDU4515+计算日期
  8. 解决TextView与RadioGroup不对齐的问题
  9. Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
  10. centos 6.4 x64安装bugfree
  11. java从文件中读取json
  12. 【BZOJ 4652】【NOI 2016】循环之美
  13. btcpool之JobMaker
  14. Maven pom.xml文件详解
  15. python 之 列表与字典
  16. Vue单元测试Karma+Mocha
  17. 《Mysql 日志结构》
  18. GO富集分析
  19. yum节省安装时间
  20. nginx unit 1.8 支持基于java servlet 的开发模型

热门文章

  1. nginx(一)- 初识
  2. [转] JAVA从本机获取IP地址
  3. C. Nice Garland-------字符串
  4. SQL注入(过滤空格和--+等注释符)
  5. PIE SDK栅格数据集的读写
  6. AWS and OpenStack
  7. 初探angular
  8. 60分钟内从零起步驾驭Hive实战学习笔记(Ubuntu里安装mysql)
  9. AQS的数据结构及实现原理
  10. SpringBoot | 第三十三章:Spring web Servcies集成和使用