假如定义形如"{}[]()"或者"{[()]}"的模式为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的更多相关文章

  1. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  2. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  4. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  5. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  6. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  7. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  9. 32. Longest Valid Parentheses (JAVA)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. HTML5 + AJAX ( 原生JavaScript ) 异步多文件上传

    这是在上篇 HTML5 + AJAX ( jQuery版本 ) 文件上传带进度条 的修改版本.后台代码不变就可以接着使用,但是脚本不再使用jQuery了,改为原生的 JavaScript 代码,所以我 ...

  2. leetcode[170]Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  3. 在ASP.NET MVC中使用JQ插件datatable

    1. Models public class Citys { public int Id { get; set; } public string CityName { get; set; } publ ...

  4. IIS安装教程

    IIS安装步骤图解: 1):打开添加删除程序,并选中添加/删除 Windows组件,后双击! 2): 选中并双击添加/删除 Windows组件后,弹出组件安装向导!并可以看到Internet 信息服务 ...

  5. UNIX网络编程中的需要注意的问题

    字节流套接字上调用read或write,输入或输出的字节数可能比请求的数量少,这个现象的原因在于内核中用于套接字的缓冲区可能已经达到了极限.此时所需要的是调用者再次调用read或write函数.这个现 ...

  6. OpenCV教程二 - Mat对象与它各种用法

    学习OpenCV大家都会遇到一个对象叫做Mat,此对象非常神奇,支持各种操作.很多初学者因此被搞得头晕脑胀,它各种用法太多太杂,搞得初学者应接不暇,感觉有心无力.无处下手之感.这里我们首先要正本清源, ...

  7. 用php 进行对文件的操作 (下)

    继续来说文件操作 新建一个文件夹或是删除一个文件夹,并不是只有右击一种方法,还可以用代码来操作 先来看没有运行代码之前的目录下有哪些文件 点击运行代码后再来看有没有增加名为aa的文件夹 添加成功 再来 ...

  8. POJ1753 搜索

    Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on ...

  9. 关于a.b和a[b]的区别

    在写代码的过程中,我们经常可以看到a.b或啊a[b],但是他们有什么区别呢: 简单说一下吧,有自己的还用群友的大力支持! 在js的对象中 var arr = {a:"b",b:&q ...

  10. Effective前端6:避免页面卡顿

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...