LeetCode OJ:Valid Parentheses(有效括号)
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.
简单的堆栈问题,代码如下:
class Solution {
public:
bool isValid(string s) {
if(!s.size())
return true;
char c;
stack<char> stk;
for(int i = ; i < s.size(); ++i){
if(s[i] == '[' || s[i] == '{' || s[i] == '('){
stk.push(s[i]);
continue;
}else if(s[i] == ']'){
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '[')
return false;
}else if(s[i] == '}'){
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '{')
return false;
}else{
if(stk.empty())
return false;
c = stk.top();
stk.pop();
if(c != '(')
return false;
}
}
if(stk.empty())
return true;
return false;
}
};
简单一点的话可以使用下面这种方式:
bool ValidParentheses(string s)
{
if (!s.size())
return true;
stack<char> stk;
map<char, char> m;
m['('] = ')';
m['['] = ']';
m['{'] = '}';
for (auto c : s){
if (c == '(' || c == '[' || c == '{'){
stk.push(c);
}
else if (c == ')' || c == ']' || c == '}'){
if (stk.empty())
return false;
if (c == m[stk.top()])
stk.pop();
else
return false;
}
}
if (stk.empty())
return true;
return false;
}
java版本代码如下所示:
public class ValidParentheses {
public static void main(String[] args) {
// TODO Auto-generated method stub
ValidParentheses validParentheses = new ValidParentheses();
String string = new String("[]{}(){[]}");
System.out.println("" + validParentheses.ValidParentheses(string));
}
boolean ValidParentheses(String str){
if(str.length() == 0)
return true;
Stack<Character> stk = new Stack<Character>();
char [] parentheses = str.toCharArray();
for(char c : parentheses){
if(c == '(' || c == '{' || c == '['){
stk.push(c);
}else if(c == ')'){
if(stk.empty() || stk.pop() != '(')
return false;
}else if(c == '}'){
if(stk.empty() || stk.pop() != '{')
return false;
}else if(c == ']'){
if(stk.empty() || stk.pop() != '[')
return false;
}
}
if(stk.empty())
return true;
return false;
}
}
LeetCode OJ:Valid Parentheses(有效括号)的更多相关文章
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- 【LeetCode】Valid Parentheses合法括号
给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]( ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
随机推荐
- lamp中的Oracle数据库链接
lamp一键安装包: https://lnmp.org/install.html 在CentOS 6.7 64位安装PHP的PDO_OCI扩展 Installing PDO_OCI extension ...
- ORACLE常用性能监控SQL(二)
查询Oracle正在执行的sql语句及执行该语句的用户 SELECT b.sid oracleID, b.username 登录Oracle用户名, b.serial#, spid 操作系统ID, p ...
- python 脚本转成exe可执行程序
本文所使用的工具是cx_Freeze,相对py2exe和PyInstaller来说,cx_Freeze的兼容性更好,支持最新的Python 3.X,支持PyQT,并且可以跨平台支持Windows和Li ...
- 动态切换数据库(EF框架)
文章简略:本文测试项目为Silverlight+EF+RIA Service动态切换数据库的问题 通常,Ado.net EntityFramework的数据库连接字符串Connect ...
- Getting Started with Rails (1)
按照官网http://guides.rubyonrails.org/getting_started.html上学习了一下例子.在过程中有很多刚开始没理解的地方,写下来. 首先,建立了一个resourc ...
- SQLAlchemy_定义(一对一/一对多/多对多)关系
目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模 ...
- PAT 天梯赛 L1-044. 稳赢 【循环】
题目链接 https://www.patest.cn/contests/gplt/L1-044 题意 根据对方给出剪刀石头步 给出对应的胜利出招,每K次 要有一个平局 思路 用一个循环,然后每K次 判 ...
- C# winform 屏蔽鼠标右键 spreadsheet Gear 屏蔽鼠标右键菜单
今天用到spreadsheetGear 插件,然后右键有插件自己的菜单.都是英文的,而且还能打开新的窗体.嵌到程序里面,不太合适,所以着手屏蔽. 刚开始用的Mouse_up,虽然能捕获事件,但是没有K ...
- Django- 反向生成url
Django中提供了一个关于URL的映射的解决方案, 1.客户端的浏览器发起一个url请求,Django根据URL解析,把url中的参数捕获,调用相应的试图,获取相应的数据,然后返回给客户端显示 2. ...
- java类执行顺序
1. 静态初始化块 > 初始化块 > 构造器 2. 父类 > 子类 综合下来顺序就是: 父类静态初始化块和静态成员变量 子类静态初始化块和静态成员变量 父类初始化块和普通成员变量 父 ...