java 使用Stack来判断Valid Parentheses
假如定义形如"{}[]()"或者"{[()]}"的模式为valid,"[{]"或者"(("的模式为invalid,那么我们可以使用一个stack或者递归下降的方法实现.
这里我先用stack实现一次.
实现的思路是.
当遇到开始符号时('[','{'或者'('),我们就将其push进栈。当遇到结束符号的时候,就pop盏里面的元素看是否匹配,否则返回false.
public boolean isValid(String s) {
char[] cs = s.toCharArray();
if (cs.length % 2 != 0)
return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<cs.length;i++){
if(cs[i]=='[' || cs[i] == '(' || cs[i] == '{'){
stack.push(cs[i]);
}else{
if(stack.isEmpty()) return false;
switch (stack.pop()){
case '(':
if(cs[i]!=')') return false;
break;
case '[':
if(cs[i]!=']') return false;
break;
case '{':
if(cs[i]!='}') return false;
break;
}
}
}
if(!stack.isEmpty()) return false;
return true;
}
java 使用Stack来判断Valid Parentheses的更多相关文章
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- 32. Longest Valid Parentheses (JAVA)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- Servlet 异步处理
web容器会为每个请求分配一个线程,Servlet3.0新增了异步处理,解决多个线程不释放占据内存的问题.可以先释放容器分配给请求的线程与相关资源,减轻系统负担,原先释放了容器所分配线程的请求,其响应 ...
- 如何用C语言封装 C++的类,在 C里面使用
本文给出了一种方法.基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在 wrapper的实现文件里实现. 1. apple.h #ifnde ...
- Oracle 12cR1 RAC 在VMware Workstation上安装(上)—OS环境配置
Oracle 12cR1 RAC 在VMware Workstation上安装(上)-OS环境配置 1.1 整体规划部分 1.1.1 所需软件介绍 Oracle RAC不支持异构平台.在同一个集群 ...
- HDU1556(树状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- RabbitMQ确认机制问题处理
现象: 手动在后台创建两个消息反馈队列 代码中监听到消息队列后,对消息进行处理并确认,代码为: 运行代码后,消息未从队列扔出去. 原因及解决方案:后台手动创建队列后,在监听消息中又对队列进行声明创建, ...
- yii2 邮件发送(有图有真相)
经典的密码找回方案是发送邮件到用户邮箱然后修改密码,下面利用yii2 高级版的mail功能,进行邮件的发送,如下图 1.在comm/config/main-local.php中添加 'mailer' ...
- MVC View显示详解(RenderBody,RenderPage,RenderSection,Partial)
一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...
- 一个想法(续四):IT技术联盟创业众筹进度公示
为了将整个创业过程更加的公开公正透明化,特开此篇用于展示众筹进度. 首轮众筹进度如下:(每天24点更新1次)
- 自动化CodeReview - ASP.NET Core请求参数验证
自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 参数验证实现 在做服务端开发 ...
- Python学习的个人笔记(基础语法)
Python学习的个人笔记 题外话: 我是一个大二的计算机系的学生,这份python学习个人笔记是趁寒假这一周在慕课网,w3cschool,还有借鉴了一些博客,资料整理出来的,用于自己方便的时候查阅, ...