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. http协议和telnet指令讲解

    http协议: 1.http:是网络传输协议:全称为:超文本传输协议: 关系:客户端和服务器的关系: 协议:就是一种规范: 常见的http和https两种,https是http的升级版 http协议: ...

  2. 7.2.4 else与if配对

    规则是,如果没有花括号,else与离它最近的if匹配,除非最近的if被花括号括起来. 注意:要缩进"语句","语句"可以是一条简单语句或复合语句. 记住,编译器 ...

  3. springboot项目如何打包成war包

    一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二.移除嵌入式tomcat插件 在pom.xml里找到spring-boot-s ...

  4. udev磁盘绑定

    udev磁盘绑定 [grid@db-rac02 ~]$ cat 99-asm-multipath.rules KERNEL=="sd*",SUBSYSTEM=="bloc ...

  5. Express路由

    1. 路由器的配置分为两个,一个是需要做页面的渲染,一个是需要直接进行对数据进行输出,对于路由器的配置需要对路由器在公共的app.js进行注册与注入才能生效,否则是不能生效的.配置时根据不同的应用场景 ...

  6. Ping 笔记

    Ping  笔记 查看网络间的连通性. 当设备与PC在同一局域网中(连接同一路由器),设备与PC互Ping对方,若只有其中一个掉线连不上,两者可能存在网络端口的问题, 若设备与PC同时掉线,则为路由器 ...

  7. hive 一次更新多个分区的数据

    类似订单数据之类的表,因为有状态要更新,比如订单状态,物流状态之类的, 这样就需要同步很久之前的数据,目前我的订单表是更新前面100天的数据. hive中操作是先删除前面100个分区的数据,然后重新动 ...

  8. 团队第十次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-15日晚上9点,以大家在群里讨论为主 召开时长10分钟 任务表格 袁勤 负责整理实验报告前后端交互,即xml文件传值部分 负责整理实验报告前后端交互 ...

  9. 【Python3学习】Python环境搭建

    以前电脑上安装过一个python2的环境,这次由于项目跟其他团队人员对的时候,人家的是python3,因为python2也没怎么学,所以对里面还是很不了解的. 所以今天就重新开始python3的学习, ...

  10. Android 开发 上拉加载更多功能实现

    实现思维 开始之前先废话几句,Android系统没有提供上拉加载的控件,只提供了下拉刷新的SwipeRefreshLayout控件.这个控件我们就不废话,无法实现上拉刷新的功能.现在我们说说上拉加载更 ...