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 ...
随机推荐
- c#单例设计模式
class Person { private static Person persons; public static Person Persons { get{ if (persons==null) ...
- Nestjs学习进度-180420
# 一.已完成:# 1.将NPM降级到4.6.1,保证Nest环境正常: 2.成功运行了示例程序:https://docs.nestjs.cn/4.6/firststeps 3.粗略看了下文档. # ...
- 【原创】Windows上应用程序报错常用分析方法总结
在日常使用Windows的过程中,经常会遇到应用程序不能正常启动.关闭等使用问题.对于Windows来说,解决这些问题的方法比较多,大多时候我们可以通过百度或谷歌搜索来解决.但更多的时候,我们需要找出 ...
- mysql免解压版安装教程步骤
首先我这里演示的是mysql-5.6.27-winx64这个免解压的版本 添加环境变量(如添加了则跳过该步骤) 操作如下: )右键单击我的电脑->属性->高级系统设置(高级)->环境 ...
- cesium3dtiles位置改变
cesium偏移3dtiles高度var heightOffset = 20.0; var boundingSphere = tileset.boundingSphere; var cartograp ...
- hive 一次更新多个分区的数据
类似订单数据之类的表,因为有状态要更新,比如订单状态,物流状态之类的, 这样就需要同步很久之前的数据,目前我的订单表是更新前面100天的数据. hive中操作是先删除前面100个分区的数据,然后重新动 ...
- Oracle不能连接故障排除[TNS-12541: TNS: 无监听程序]
1. 情况:Oracle安装后使用的好好的,放假关机后重新开机居然发现不能正常连线,客户报错:TNS-12541: TNS: 无监听程序 2. 检查: 1. 服务器上Oracle启动正 ...
- JS面试Q&A(续2): Rest parameter,Arrow function 等
rest parameter 和 Destructuring assignment. function fun1(...theArgs) { console.log(theArgs.length);} ...
- post提交参数过多时,取消Tomcat对 post长度限制
1.Tomcat 默认的post参数的最大大小为2M, 当超过时将会出错,可以配置maxPostSize参数来改变大小. 从 apache-tomcat-7.0.63 开始,参数 maxPostSiz ...
- 分布式 基本理论 CAP 2
关于P P, 即 Partition字面意思是网络分区,其实 包括了 各种网络问题, 我们要把它理解 一个 广义的 分区问题. P 涉及到了 时间, 这么说吧, 出现了分区, 那就是节点之间 “长久的 ...