【LeetCode】20 - Valid Parentheses
2015-09-09 21:05
267 查看
Given a string containing just the characters
The brackets must close in the correct order,
Tags: Stack String
Solution:
'(',
')',
'{',
'}',
'['and
']', determine if the input string is valid.
The brackets must close in the correct order,
"()"and
"()[]{}"are all valid but
"(]"and
"([)]"are not.
Tags: Stack String
Solution:
class Solution { public: bool isValid(string s) { stack<char> stk; for(int i=0;i<s.size();i++){ char c=s[i]; if(c=='('||c=='['||c=='{'){ stk.push(c); }else if(c==')'||c=='}'||c==']'){ if(stk.empty())return false; else{ char c2=stk.top(); stk.pop(); if((c2=='('&&c!=')')||(c2=='{'&&c!='}')||(c2=='['&&c!=']'))return false; } } } return stk.empty(); } };
相关文章推荐
- java环境变量详解---找不到或无法加载主类
- JAVA的第一篇博客,为什么这么激动!!!!
- java学习之认识泛型
- 手把手教你把Vim改装成一个IDE编程环境(图文)
- Struts2不可忽视的基础
- java设计模式学习笔记--可配置的简单工厂模式
- UILabel
- hdu1730Northcott Game nim
- Hello JSP!——JSP内置对象分类及属性保存范围
- vi中常用命令
- Camera
- 0909 对编译原理的看法
- 字节对齐C++
- 浅谈面向对象第三枪
- 浅谈Linux文件系统
- unity实现简单的摄像机切换
- 软件体系结构————防御性编程
- oracle查看是否启用归档日志
- leetcode:Basic Calculator II
- 让我细细捋一捋原码反码补码以及计算机中负数的表示