[LeetCode]20. Valid Parentheses有效的括号
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
要求判断括号是否匹配,我们用一个stack来存储,然后用map存储对应的括号序列
每次往栈里添加元素时,通过map判断是左括号[还是右括号],如果是右括号,可以通过map判断出来,就从
stack中pop出栈顶元素来和当前括号匹配,如果不匹配则说明整个序列都不匹配,如果是左括号就继续加入到stack中
class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<Character>();
Map<Character,Character> map=new HashMap<Character,Character>();
map.put(')','(');
map.put('}','{');
map.put(']','[');
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(map.containsKey(c)){
char temp=stack.empty()? '#':stack.pop();
if(temp!=map.get(c)) return false;
}else stack.push(c);
}
return stack.isEmpty();
}
}
[LeetCode]20. Valid Parentheses有效的括号的更多相关文章
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- myeclipse2014 安装maven3.3.9和maven配置本地仓库 及错误修改
结合网上的知识梳理以及自己安装的经验 myeclipse2014 安装maven3.3.9和maven配置本地仓库 及犯的错误修改 成功搞定maven 1,安装 Maven 之前要求先确定你的 J ...
- Django 学习:为窗体加上防机器人的验证机制(验证码功能)
这里我们使用 django-simple-captcha 模块,官方介绍如下:https://github.com/mbi/django-simple-captcha 一键安装: pip instal ...
- 洛谷P2774 方格取数问题(最小割)
传送门 考虑一下,答案就是全局和减去舍弃和 不难发现,如果我们按行数+列数的奇偶性分为两类,那么每一类中的数必然互不相邻 那么我们把原图的点分为黑点和白点两类,原地向白点连边,黑点向汇点连边,容量为点 ...
- Django REST framework序列化
一.简介 Django REST framework是基于Django实现的一个RESTful风格API框架,能够帮助我们快速开发RESTful风格的API. 官网:https://www.djang ...
- Unity---动画系统学习(4)---使用混合树(Blend Tree)来实现走、跑、转弯等的动画切换
1. 介绍 Blend Tree用于多个动画之间的混合,比如走到跑的切换.转弯的切换. 如果用动画学习笔记(3)中的方法,需要新建很多的状态,不仅麻烦,而且切换状态时也很容易不流畅. 而Blend T ...
- 【AT2434】JOI 公園 (JOI Park) 最短路+贪心
题解 我的歪解 我首先想的是分治,我想二分肯定不行,因为它是没有单调性的. 我想了一下感觉它的大部分数据应该是有凸性的(例如\(y=x^2\)的函数图像),所以可以三分. 下面是我的三分代码(骗了不少 ...
- 《Andrew Ng深度学习》笔记4
浅层神经网络 1.激活函数 在神经网络中,激活函数有很多种,常用的有sigmoid()函数,tanh()函数,ReLu函数(修正单元函数),泄露ReLu(泄露修正单元函数).它们的图形如下: sigm ...
- 传智播客Springmvc_mybatis学习笔记
文件地址:https://download.csdn.net/download/qq_26078953/10614459
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- CDN基本原理和功能浅析
CDN的全称是Content Delivery Network,即内容分发网络.CDN的通俗理解就是网站加速,CPU均衡负载,可以解决跨运营商,跨地区,服务器负载能力过低,带宽过少等带来的网站打开速度 ...