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 ...
随机推荐
- C语言在32位和64位机器下数字数据类型的字节数取决于编译器和平台, 主要由编译器决定。
C语言中数字数据类型的字节数 C声明 32位机器 64位机器 char 1 1 short int 2 2 int 4 4 //整型在32位和64位下都是4个字节 long int 4 8 l ...
- HTML5实用知识点
本文讲解HTML5实用知识点 新增的表单type Canvas使用 SVG使用 Audio使用 Video使用 网页缓存 文件缓存 后台worker Server-Sent Events 定位 拖放功 ...
- wamp集成环境下mysql数据库的分开部署和远程访问
今天折腾了一天一个小问题,就是明明正确的php代码在访问数据库的时候总是提示DB ERROR.后来才发现是填写数据库名的时候,写成了该数据库的ip地址(其实也是本机ip但是本机还是不能访问),而不是l ...
- Shell编程进阶 2.1 shell函数
函数 vim fun.sh #!/bin/bash function mysum() { sum=$[$+$] echo $sum } a= b= mysum $a $b sh fun.sh 6 可以 ...
- Android 媒体编解码器(转)
媒体编解码器 MediaCodec类是用来为低级别的媒体编码和解码的媒体编解码器提供访问.您可以实例化一个MediaCodec类通过调用createEncoderByType()方法来进行对媒体文件进 ...
- linux动态内核模块编程-3
将一组与模块相关的命令加载进内核 完成功能类似2,打印proc下的相关信息.但是不用重新编译内核,节省时间,更为灵活 内核模块介绍 模块是在内核空间运行的程序,实际上是一种目标文件,不能单独运行但其代 ...
- noi.ac day1t3 Sort
传送门 分析 快排的原理是以任意一个数为标准,然后把所有小于它的数换到它的左边,所有大于它的数换到它的右边.我们就使用快排的思路,分治整个区间.对于每个区间以排好序的这个数列的中间位置的值为标准,然后 ...
- 第三章:PCL基础3.1
架构师为了确保在PCL中所有代码风格的一致性,使得其他开发者及用户容易理解源码,PCL开发者制定并遵循着一套严格的编写规范,PCL的开发者都默认此规范. 3.1PCL推荐的命名规范 1.文件命名 1) ...
- Pig Latin程序设计1
Pig是一个大规模数据分析平台.Pig的基础结构层包括一个产生MapReduce程序的编译器.在编译器中,大规模并行执行依据存在.Pig的语言包括一个叫Pig Latin的文本语言,此语言有如下特性: ...
- Entity Framework Tutorial Basics(25):Delete Single Entity
Delete Entity using DBContext in Disconnected Scenario: We used the Entry() method of DbContext to m ...