20. Valid Parentheses (JAVA)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- 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)的更多相关文章
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- maven私库nexus2.3.0-04迁移升级到nexus-3.16.1-02(异机迁移备份)
环境信息: nexus2.3.0-04安装在32位Windows server 2003系统上 安装位置信息如下: 仓库迁移 Nexus的构件仓库都保存在sonatype-work目录中,nexus2 ...
- springboot集成schedule
背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...
- aspose.cells 复制单元格
将第1行至第27行复制到第28行 cells.CopyRows(cells, 0, i*27, 27);
- springboot项目如何打包成war包
一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二.移除嵌入式tomcat插件 在pom.xml里找到spring-boot-s ...
- idea spring boot docker 多项目 maven 编译
1,重复的model [INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while proc ...
- iptables和netfilter
1.iptables和netfilter说明 [1]netfilter/iptables组成Linux平台下的包过滤防火墙,iptables是用户空间的管理工具,netfilter是内核空间的包处理框 ...
- centos 6.5 安装redis
1. 下载redis,编译安装 下载地址:https://redis.io/download(建议大家都选择稳定版本) 下载到本地,然后上传到集群 当然也可以通过命令行直接在线下载 $ wget ht ...
- VMware12 安装 Mac OS 10.12 步骤及设置优化教程
最近公司要开发苹果的ARKit应用,但是项目组穷啊,只有美工用着一台苹果本本,所以肯定不能老用他的电脑,效率低还老打扰他.所以我就想着用虚拟机整,毕竟玩了N年的虚拟机了,应该是没啥问题的.所以就开始各 ...
- C#导出Excel后关闭进程EXCEL.EXE
在C#中使用Microsoft.Office.Interop.Execl 导出excel 表格时,将以下两个属性亩后,在导完后, Excel.exe 进程无法关闭. // excel app 是否可见 ...
- [UnityShader效果]01.Mask
参考链接: https://blog.csdn.net/akof1314/article/details/50428200 1.Mask.shader // Unity built-in shader ...