题目描述: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. SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂)

    SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂) 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏/高词汇.访问权限控制(URL级别 ...

  2. mysql数据库——事务隔离级别

    四种隔离级别: 一:READ UNCOMMITTED(未提交读) 事务可以读取其他事务未提交的数据,称为脏读 二:READ COMMITTED(提交读) 一个事务开始时,只能"看见" ...

  3. PASS模型-第一周个人报告

    PASS模型-第一周个人报告 博客班级 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus ...

  4. Java_大体介绍(超级短的那种)

    Java三大版本 Java SE: Java Standard Edition, 定位于客户端, 用于桌面应用软件编程 Java ME: Java Micro Edition, 用于嵌入式系统开发 J ...

  5. SWT JFace 小制作 文本阅读器

    1 package swt_jface.demo11; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.i ...

  6. Azure Data Factory(四)集成 Logic App 的邮件通知提醒

    一,引言 上一篇有介绍到使用Azure Data Factory 复制数据,然后有集成 Azure DevOps 实现CI/CD,但是对于真正的项目来说,这些肯定是不够的,比如说在执行 Azure P ...

  7. pc端兼容IE9及以上版本

    最近业务部门反映我们商城的兼容性不是很好,尤其是在IE浏览器上,经过调研,我们决定对IE9及以上版本的IE内核浏览器进行主流程测试,发现有哪些功能在IE9上不兼容 一.CSS兼容性 如下图所示 使用了 ...

  8. cmd,py脚本,py编译的exe,uipath及uibot对它们的调用

    UIPATH调用Python编译程序exe 好处: 1)code不以可编辑的状态被用户接触,对于不懂反编译的一般用户,可提升一定的代码安全性: 2)不需要用户机器上安装 python环境. 3)可以将 ...

  9. python插入数据库mysql

    #-*- coding:utf-8 -*- import MySQLdb #alter table test add index prefixIdx_test(ext(2));//前缀索引 try: ...

  10. binary hacks读数笔记(readelf命令)

    可以用readelf命令来查看elf文件内容,跟objdump相比,这个命令更详细. 1. readelf -h SimpleSection.o ELF Header: Magic: 7f 45 4c ...