给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
括号必须以正确的顺序关闭,"()" 和 "()[]{}" 是有效的但是 "(]" 和 "([)]" 不是。
详见:https://leetcode.com/problems/valid-parentheses/description/

实现语言:Java

class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
Character now;
Character prev;
int length=s.length();
for(int i=0;i<length;i++){
now=s.charAt(i);
if(now=='('||now=='['||now=='{'){
stack.push(now);
}else{
if(stack.isEmpty()){
return false;
}else{
prev=stack.pop();
} if(now==')'&&prev!='('){
return false;
}else if(now==']'&&prev!='['){
return false;
}else if(now=='}'&&prev!='{'){
return false;
}
}
}
return stack.isEmpty();
}
}

参考:https://blog.csdn.net/runningtortoises/article/details/45621933

http://www.cnblogs.com/grandyang/p/4424587.html

020 Valid Parentheses 有效的括号的更多相关文章

  1. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  2. LeetCode 020 Valid Parentheses

    题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...

  3. LeetCode OJ:Valid Parentheses(有效括号)

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

  4. [LeetCode]20. Valid Parentheses有效的括号

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

  5. 【LeetCode】020. Valid Parentheses

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

  6. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  7. 【JAVA、C++】LeetCode 020 Valid Parentheses

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

  8. [Leetcode][020] Valid Parentheses (Java)

    题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...

  9. leetcode 20 Valid Parentheses 有效的括号

    描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...

随机推荐

  1. Poj 2602 Superlong sums(大数相加)

    一.Description The creators of a new programming language D++ have found out that whatever limit for ...

  2. kvm 基础 虚拟机改名

    转自:http://www.cnblogs.com/5201351/p/4464350.htm 1.查看所有的kvm虚拟机 [root@5201351_kvm ~]# virsh list --all ...

  3. 在Action获取Scope对象

    引言:在前面的Action操作中,关键就是Action中的exectue方法,但是此方法并没有request.session.application等对象作为参数,自然就不能利用这些对象来操作.下面我 ...

  4. [matlab]bp神经网络工具箱学习笔记

    基本就三个函数: newff():创建一个bp神经网络 train():训练函数 sim():仿真函数 同时具有可视化界面,但目前不知道可视化界面如何进行仿真,且设置不太全 工具箱:Neural ne ...

  5. mvn jetty:run 出现PermGen space outofmemeryerror

    使用mvn jetty:run跑别人的项目时出现了PermGen space outofmemeryerror异常 http://stackoverflow.com/questions/1451648 ...

  6. hbase-0.98.1-cdh5.1.0伪分布式安装

    分三步: 1. 添加环境变量 2.编辑hbase-env.sh文件 3.编辑hbase-site.xml 文件 前提条件是安装好hadoop,下面展开说明 1. 添加环境变量 export HBASE ...

  7. java连接sqlserver2005数据库

    java连接sqlserver2005数据库   首先得下载驱动程序到微软网站下载Microsoft JDBC Driver 4.0 for SQL Server 下载地址 :http://msdn. ...

  8. 各版本Google浏览器下载地址

    各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/

  9. ibatis分页的两种方式

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  10. 诺基亚 920T - 我的非凡系列手机始终显示旋转齿轮而无响应,我该如何让手机停止显示旋转齿轮?

    有时,在 OTA 更新 (或重置手机) 后,设备可能始终显示“旋转齿轮”而无响应. 如果“旋转齿轮”在屏幕上显示的时间超过 60 分钟,则需要执行恢复操作. 您可以尝试下面这些简单的解决方法: 按住电 ...