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. delphi VCL组件同名继承

    当我们在扩展一个 vcl 组件功能的时候,既想保留IDE中能拖动大小与直接设置属性的功能,又想减少写创建与释放代码和安装扩展后新组件的麻烦,那么本文中的方法,就非常实用了. 以给TStringGrid ...

  2. SPARC T4 RAID Setup (ZT)

    http://www.confignotes.com/2013/09/sparc-t4-raid-setup/ How to configure a RAID volume with the inte ...

  3. Log4j 记录error 日志

    第一个bug的起始,是在线上日志发现一个频繁打印的异常——java.lang.ArrayIndexOutOfBoundsException.但是却没有堆栈,只有一行一行的ArrayIndexOutOf ...

  4. javascript——屏蔽右键快捷菜单

    JS: function menufalse(){ return false; } document.oncontextmenu = menufalse; //禁用快捷菜单 Jquery: $(&qu ...

  5. 11-18网页基础--第二部分CSS样式属性(1)

    第一课:样式属性 style 样式:style样式不仅可以直接在<body>中设置成整个网页的样式.格式:为了将样式.格式多样化,也可以将style单独抽出来,作为一个独立的个体,放在&l ...

  6. mysql in 方法查询 按照 in队列里的顺序排序

    String sql ' GROUP BY comm " + "order by field(comm,?,?,?,?,?,?,?,?)"; stmt = conn.pr ...

  7. 通过Excel导入Mysql 超过65535条数据的办法

    1.截取 65534条数据,进行分sheet,然后1个sheet导入一张表,最后进行整合! 2.采用TXT导入方式,TXT的导入暂时没发现限制的数据条数,下午用TXT导入74万条数据成功 3.如果遇到 ...

  8. C程序设计语言(K&R) 笔记1

    当作复习... (1)将华氏度 换算成 摄氏度,公式: ℃=(5/9)(̧°F-32) #include <stdio.h> int transformTemprature(int F){ ...

  9. jquery.pagination.js使用

    直接上代码: <script type="text/javascript"> var pageIndex = 1; //页面索引初始值 var pageSize = 1 ...

  10. JavaScript的编译原理

    尽管通常将 JavaScript 归类为“动态”或“解释执行”语言,但事实上它是一门编译语言.这个事实对你来说可能显而易见,也可能你闻所未闻,取决于你接触过多少编程语言,具有多少经验.但与传统的编译语 ...