2.Valid Parentheses (括号匹配)
Level:
Easy
题目描述:
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.
思路分析:
简单的括号匹配问题,用map保存三种括号的对应关系,同时设置一个栈,遍历字符串,如果遇到的是左括号,那么入栈,如果遇到的是右括号,判断栈顶的符号是不是其对应的左括号,如果是则栈顶弹出,如果不是则括号不匹配,遍历完字符串后判断栈是否为空,如果为空则括号匹配,如果不为空,则不匹配。
代码:
class Solution {
public boolean isValid(String s) {
HashMap<Character,Character>map=new HashMap<>();
map.put('(',')');
map.put('[',']');
map.put('{','}');
Stack<Character>stack=new Stack<>();
for(int i=0;i<s.length();i++){
if(stack.isEmpty()){
if(s.charAt(i)==')'||s.charAt(i)==']'||s.charAt(i)=='}')
return false;
else
stack.push(s.charAt(i));
}else{
if(s.charAt(i)==map.get(stack.peek()))
stack.pop();
else if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{')
stack.push(s.charAt(i));
else
return false;
}
}
if(stack.isEmpty())
return true;
else
return false;
}
}
2.Valid Parentheses (括号匹配)的更多相关文章
- 20. Valid Parentheses(括号匹配,用桟)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 20. Valid Parentheses - 括号匹配验证
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- #4 div1E Parentheses 括号匹配
E - Parentheses Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Subm ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 32. Longest Valid Parentheses(最长括号匹配,hard)
Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- JavaScript基本概念A
简介 如果需要了解这些概念, 您应该熟悉 JS 的基本知识. 弱类型 在也无需绞尽脑汁觉得到底采用 float.double,int 还是 short 或是 long 还是 String.只需这样定义 ...
- CreateMutex实现只能打开一个客户端
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace s ...
- linux命令-mke2fs
想在磁盘下写东西,必须要先格式化 /////////////////////////////////////////////////////////////////////////////////// ...
- hibernate学习笔记(4)表单操作
User.hbm.xml的表单配置: ①主键 <id name="id" type="java.lang.Integer"> <column ...
- UAC(User Agent Client) 和 UAS(User Agent Server)
SIP协议采用Client/Server模型.每一个请求(Request)触发服务器的一个操作:每个操作被称为方法(Method):每个请求除了指明具体方法外,还携带了一系列的头域(Header fi ...
- Angular18 RXJS
1 RX 全称是 Reactive Extensions,它是微软开发并维护的基于 Reactive Programming 范式实现的一套工具库集合:RX结合了观察者模式.迭代器模式.函数式编程来管 ...
- 64位系统中fatal error: stdio.h: 没有那个文件或目录的错误的解决方法
Ubuntu系统中可输入如下命令,安装开发环境: sudo apt-get install build-essential https://blog.csdn.net/yygydjkthh/artic ...
- java线程安全的元素
集合类: Vector Stack HashTable enumeration 字符串: StringBuffer
- struts1和struts2之间的区别
从action类上分析:1.Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口. 2. Struts 2 Action类可以实现一个Action接 ...
- html 连接数据库
http://blog.csdn.net/haxker/article/details/4214001 http://www.cnblogs.com/chuncn/archive/2010/11/22 ...