题目描述:Valid Parentheses

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

分析:

代码如下:

class Solution {
public:
bool isValid(string s) { string left = "([{";
string right = ")]}";
stack<char> stk; for(auto c : s){ //如果是"([{",则压入栈中
if(left.find(c) != string::npos){
stk.push(c);
}
//如果不是"([{",则匹配
else{
if(stk.empty() || stk.top() != left[right.find(c)])
return false;
else stk.pop();
}
} return stk.empty(); }
};

Java:

    public boolean isValid(String s) {

        HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('(', ')');
map.put('[', ']');
map.put('{', '}'); Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i); if (map.keySet().contains(curr)) {
stack.push(curr);
} else if (map.values().contains(curr)) {
if (!stack.empty() && map.get(stack.peek()) == curr) {
stack.pop();
} else {
return false;
}
}
} return stack.empty(); }

LeetCode 020 Valid Parentheses的更多相关文章

  1. 【JAVA、C++】LeetCode 020 Valid Parentheses

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

  2. [Leetcode][020] Valid Parentheses (Java)

    题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...

  3. [LeetCode] Longest Valid Parentheses 最长有效括号

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

  4. [LeetCode] Longest Valid Parentheses 解题思路

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

  5. [Leetcode] longest valid parentheses 最长的有效括号

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

  6. [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉

    (Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...

  7. [LeetCode] 20. Valid Parentheses 验证括号

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

  8. [LeetCode] 20. Valid Parentheses 合法括号

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

  9. [LeetCode] Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

随机推荐

  1. python使用redis缓存数据库

    Redis 关注公众号"轻松学编程"了解更多. Windows下直接解压可用,链接:https://pan.baidu.com/s/1rD4ujoN7h96TtHSu3sN_hA ...

  2. 配置JSTL 解决错误:org.apache.jasper.JasperException

    在IDEA中SSM项目开发中, 在TomCat部署ssm项目时,JSTL的错误 错误如下: 1 org.apache.jasper.JasperException: The absolute uri: ...

  3. 计算机二级考试:Java

    目录 第 1 章 Java 语言概论 第 2 章 基本数据类型 2.1 概述 2.1.1 标识符 2.1.2 关键字 2.1.3 常量 2.2 基本数据类型 第 3 章 运算符和表达式 3.2 算术运 ...

  4. MapStruct 解了对象映射的毒

    前言 MVC模式是目前主流项目的标准开发模式,这种模式下框架的分层结构清晰,主要分为Controller,Service,Dao.分层的结构下,各层之间的数据传输要求就会存在差异,我们不能用一个对象来 ...

  5. Dynamic 365 学习(1)

    一 创建解决方案 1.点击下拉菜单 2.找到设置并选择 3.点击解决方案 进入解决方案  该页面会显示你的所有的解决方案  你新建之后的可以在这里进行查看,也可以新增 删除  ... 这里我们先新建一 ...

  6. 依赖注入DI(IOC)容器快速入门

    1.什么是IOC IOC是一种设计模式,全程控制翻转或叫依赖注入.更详细介绍见http://martinfowler.com/articles/injection.html 2.为什么用IOC 我们通 ...

  7. 通过JS判断当前浏览器的类型

    通过JS判断当前浏览器的类型,对主流浏览器Chrome.Edge.Firefox.UC浏览器.QQ浏览器.360浏览器.搜狗浏览器的userAgent属性值来判断用户使用的是什么浏览器. 不同浏览器的 ...

  8. CSS实现模拟百度分享侧边栏效果

    在<JS模拟百度分享侧边栏效果>一文中对于Div区块的运动通过JS实现了鼠标移入滑出显示,鼠标移出滑入隐藏的效果.其实在CSS3中通过transition属性就可以较为轻松实现. < ...

  9. Socket accept 简要分析

    accept 用于从指定套接字的连接队列中取出第一个连接,并返回一个新的套接字用于与客户端进行通信,示例代码如下 #include <sys/types.h> /* See NOTES * ...

  10. TIME_WAIT状态存在的原因

    TIME_WAIT状态存在有两个理由: 1.可靠地实现TCP全双工连接的中断 2.允许老的重复分节在网络中消失 第一个理由:如果客户端的ACK丢失了,服务器将会重新发送它的最终的那个FIN,因此客户端 ...