Level:

​  Easy

题目描述:

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. 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 (括号匹配)的更多相关文章

  1. 20. Valid Parentheses(括号匹配,用桟)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. 20. Valid Parentheses - 括号匹配验证

    Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  3. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  4. #4 div1E Parentheses 括号匹配

    E - Parentheses Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Subm ...

  5. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. 32. Longest Valid Parentheses(最长括号匹配,hard)

      Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...

  7. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  8. Longest Valid Parentheses(最长有效括号)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. angular使用代理解决跨域

    angular2.angular4.angular5 及以上版本的跨域问题. 通过angular自身的代理转发功能 配置package.json 两种方式启动代理服务 第一种: 启动项目通过npm s ...

  2. Android 4学习(5):概述 - Android应用程序的生命周期

    参考:<Professional Android 4 Application Development> Android应用程序生命周期 Android应用程序无法控制自己的生命周期,因此它 ...

  3. hadoop 常用端口 及模块介绍

    50070                 namenode http port 50075                 datanode   http  port 50090          ...

  4. NSThread 基本使用

    一.简介 (1)使用NSThread对象建立一个线程非常方便 (2)但是!要使用NSThread管理多个线程非常困难,不推荐使用 (3)技巧!使用[NSThreadcurrentThread]跟踪任务 ...

  5. [Python Study Notes]计算器

    # ------------------------------------------------------------------------------------- # @文件: 计算器.p ...

  6. 【总结整理】IFeatureBuffer

    IFeatureBuffer pRowBuffer = objTabWYDCQ_Tar.CreateFeatureBuffer(); pRowBuffer.Shape = SourceRow.Shap ...

  7. php环境引起的"syntax error unexpected $end"

    今天在网上淘了一段php和javascript的二级联动下拉菜单的代码.本地运行的时候,一直提示错误 syntax error unexpected $end 本来还以为是括号或者标签没有用好,但是仔 ...

  8. iOS UITableView制作类似QQ好友列表视图

                #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDele ...

  9. hibernate 对象OID

    它是hibernate用于区分两个对象是否是同一个对象的标识. 我们都知道,虚拟机内存区分两个对象看的是内存的地址是否一致.数据库区分两个对象,靠的是表的主键.hibernate负责把内存中的对象持久 ...

  10. opencv reshape函数说明

    转自http://blog.csdn.net/yang6464158/article/details/20129991 reshape有两个参数: 其中,参数:cn为新的通道数,如果cn = 0,表示 ...