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.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false 正常的代码
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='('||c=='['||c=='{')
st.push(c);
else if(c=='}'&&!st.empty()&&st.peek()=='{')
st.pop();
else if(c==']'&&!st.empty()&&st.peek()=='[')
st.pop();
else if(c==')'&&!st.empty()&&st.peek()=='(')
st.pop();
else
return false;
}
return st.empty();
}
}
很巧妙的方法
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<Character>();
for(char c:s.toCharArray()){
if(c=='(')
st.push(')');
else if(c=='[')
st.push(']');
else if(c=='{')
st.push('}');
else if(st.empty()||st.pop()!=c)
return false;
}
if(st.empty())
return true;
else
return false;
}
}

Leetcod--20. Valid Parentheses(极简洁的括号匹配)的更多相关文章

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

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

  2. 20. Valid Parentheses[E]有效的括号

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

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

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

  4. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  5. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  6. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  7. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  8. 刷题20. Valid Parentheses

    一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...

  9. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

随机推荐

  1. 10.17JS日记

    1.变量提升 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域,叫window, window分为两个模块,一个叫做内存模块,一个叫做运行模块,内存模块找到当前作用域下的 ...

  2. C++中的set

    总结一下: vector:封装了数组  list:封装了列表  map,set:封装了二叉树 set:用来存储同一类型的数据类型 非关联容器相对关联型容器插入效率高,原因是:不需要内存拷贝和内存移动, ...

  3. bowtie:短序列比对的新工具

    bowtie:短序列比对的新工具(转) (2014-11-17 22:15:24) 转载▼ 标签: 转载   原文地址:bowtie:短序列比对的新工具(转)作者:玉琪星兆 Bowtie是一个超级快速 ...

  4. phpstudy报告80端口被占用

    将所有的80端口修改为81后重启,还是不行

  5. &和&&的区别是什么

    1)&是按位与操作符,a&b是把a和b都转换成二进制数后,然后再进行按位与的运算.&&是逻辑与运算符,a&&b就是当且仅当两个操作数都为true时,其结 ...

  6. java指针与引用(转载)

    大家都知道java和C#中没有指针这个概念.但是也导致了编程中常常忽略了对象与引用的区别,难道java真的没有指针吗?句柄是什么?变量地址在哪里?没有地址是不可能的,关键是java中如何避免了指针这个 ...

  7. ubuntu系统ftp连接 以及ssh连接

    tfp连接 ssh连接 ubuntu下ssh使用 与 SCP 使用 1 ssh远程登录服务器 ssh username@remote_ip #将username换成自己的用户名,将remote_ip换 ...

  8. 码云-中国的github

    # 域名城 https://www.domain.cn/ # 码云 https://gitee.com

  9. 很漂亮的IP头格式

    IP数据包格式 TCP/IP协议定义了一个在因特网上传输的包,称为IP数据报(IP Datagram).这是一个与硬件无关的虚拟包,由首部和数据两部分组成.首部的前一部分是固定长度,共 20 字节,是 ...

  10. java.security.cert.CertificateException: No subject alternative names matching IP address xxx.xxx.xxx.xxx found

    https与http不同的是,https加密,需要验证证书,而http不需要. 在连接的代码中加上: static { disableSslVerification(); } private stat ...