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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); //Primitive types such as char cannot be used as type parameters in Java. You need to use the wrapper type for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){
stack.push(s.charAt(i));
}
else if(s.charAt(i) == ')') {
if(stack.empty() || stack.peek() != '(') return false;
stack.pop();
}
else if(s.charAt(i) == ']'){
if(stack.empty() || stack.peek() != '[') return false;
stack.pop();
}
else if(s.charAt(i) == '}'){
if(stack.empty() || stack.peek() != '{') return false;
stack.pop();
}
}
if(!stack.empty()) return false;
return true;
}
}

20. Valid Parentheses (JAVA)的更多相关文章

  1. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  2. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  3. 20. Valid Parentheses【leetcode】

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

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  7. 刷题20. Valid Parentheses

    一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...

  8. C# 写 LeetCode easy #20 Valid Parentheses

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

  9. [LeetCode] 20. Valid Parentheses 验证括号

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

随机推荐

  1. C#控制台下测试多线程的源码

    下边代码是关于C#控制台下测试多线程的的代码,应该是对小伙伴有所用. class Program { static void Main(string[] args) { ThreadStart num ...

  2. ubuntu16.04 pip install scrapy 报错处理

    Failed building wheel for Twisted inculde/site/python3./Twisted failed with error code in tmp/pip-in ...

  3. 注解 - Excel 校验工具

    注解类: @Retention(RetentionPolicy.RUNTIME) public @interface ExcelValidate { public boolean ignoreBlan ...

  4. 使用yum安装 gdb/g++等软件包

    报错: Cannot find a valid baseurl for repo: base/7/x86_6 解决方法: 方法一. 1.打开 vi /etc/sysconfig/network-scr ...

  5. 初见Hadoop—- 搭建MyEclipse 访问HDFS 上的文件

    因公司项目需要,开始接触大数据分析这块知识.网上关于大数据这块的知识还是比较多的.学习了一个礼拜了,再次记录一下,自己的学习过程,希望可以帮助后学者少走一些弯路. 服务端的配置,由于公司项目经理已经配 ...

  6. 2018-2019-2 20165205《网络对抗技术》Exp4 恶意代码分析

    2018-2019-2 20165205<网络对抗技术>Exp4 恶意代码分析 实验要求 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中 ...

  7. springcloud相关资料收集

    http://springboot.fun/  Spring  Boot 中文索引 http://springcloud.fun/   Spring Cloud 中文索引 https://spring ...

  8. TestNG 单元测试框架的使用

    JUnit让开发人员了解测试的实用性,尤其是在单元测试这一模块上比任何其他测试框架都要简单明了.凭借一个相当简单,务实,严谨的架构,JUnit已经能够“感染”了一大批开发人员.TestNG是一个测试框 ...

  9. orcal - 子查询

    where 单行单列 SELECT AVG(sal) from emp; SELECT * FROM EMP WHERE sal <(SELECT AVG(sal) from emp); 单行多 ...

  10. 关于python的一些想法

    我来自信息管理与信息系统专业,大一学过c语言但不太精通.学习python是为了学会这门新语言,据了解python会慢慢成为主流编程语言. 因为对绘图方面很感兴趣,希望老师能够在课上多讲一些这方面的东西 ...