20.Valid Parentheses
2015-06-06 11:57
316 查看
思路:普通的栈
class Solution {
public:
bool isValid(string s) {
stack<char> the_stack;
for(int i=0;i<s.size();++i){
if(s[i]=='('|| s[i]=='{'|| s[i]=='['){
the_stack.push(s[i]);
}else if(!the_stack.empty()){
char pre=the_stack.top();
if( (s[i]==')' && pre=='(') || (s[i]==']' && pre=='[') || (s[i]=='}' && pre=='{') ){
the_stack.pop();
}else{
return false;
}
}else{
return false;
}
}
if(the_stack.empty())
return true;
else
return false;
}
};
class Solution {
public:
bool isValid(string s) {
stack<char> the_stack;
for(int i=0;i<s.size();++i){
if(s[i]=='('|| s[i]=='{'|| s[i]=='['){
the_stack.push(s[i]);
}else if(!the_stack.empty()){
char pre=the_stack.top();
if( (s[i]==')' && pre=='(') || (s[i]==']' && pre=='[') || (s[i]=='}' && pre=='{') ){
the_stack.pop();
}else{
return false;
}
}else{
return false;
}
}
if(the_stack.empty())
return true;
else
return false;
}
};
相关文章推荐
- okhttp
- C#从基于FTPS的FTP server下载数据 (FtpWebRequest 的使用)SSL 加密
- opencv HOG中detectMultiScale函数详解
- 程序员必备,C#各类项目、开源项目插件资料收藏
- Oracle connect by prior理解
- 用VS2010检查两个MSSQL数据库的架构差异并同步
- 0. chromium源代码分析 - 序
- 字符串倒序
- [FZYZOJ 1003] 弱弱的战壕
- 小鹰展翅_虚函数
- 如何提升你的能力?给年轻程序员的几条建议
- KVO的坑
- Android开发的IDE、ADT、SDK、JDK、NDK等名词解释
- docker 自制镜像
- 代码片_笔记
- Java 理论与实践: 正确使用 Volatile 变量
- HDU1011
- CSDN-markdown编辑器使用说明
- Python ConfigParser模块教程
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]