题目描述: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. Scrapy加Redis加IP代理池实现音乐爬虫

    音乐爬虫 关注公众号"轻松学编程"了解更多. 目的:爬取歌名,歌手,歌词,歌曲url. 一.创建爬虫项目 创建一个文件夹,进入文件夹,打开cmd窗口,输入: scrapy star ...

  2. 关于windows下activeMQ的安装

    1.下载地址http://activemq.apache.org/activemq-5154-release.html 2.修改登录账号和密码,在配置文件jetty-realm.properties中 ...

  3. Masking Personal Information

    Masking Personal Information We are given a personal information string S, which may represent eithe ...

  4. Centos中部署NetCore项目(一)

    前言 本文是基于centos8,dotnetcore3.1. (为了服务器安全使用,程序部署最好不要直接root账号进行操作.) 如果使用sudo命令时候,提示用户不在sudoers文件中的解决方法. ...

  5. python开发--python函数-(持续更新)

    1. 打印 : print() # 打印,输出 2. 变量 : var = 'hello' # 变量var , 把'hello' 赋值给变量 var 3. if 函数 : # 代码块 4个空格或者一个 ...

  6. leetcode116:search-for-a-range

    题目描述 给出一个有序数组,请在数组中找出目标值的起始位置和结束位置 你的算法的时间复杂度应该在O(log n)之内 如果数组中不存在目标,返回[-1, -1]. 例如: 给出的数组是[5, 7, 7 ...

  7. 虚拟机中安装Centos 7

    VMware中安装centos7系统 一.首先需要准备必要文件 1.VMware软件的安装包,建议使用12以上版本 VMwareWorkstation14版本下载链接 链接:https://pan.b ...

  8. [MIT6.006] 16. Dijkstra

    先回顾下上节课的内容: 下面来看一个定理:对于所有的点来说,放松操作总是满足 d[v] ≥ δ(s, v).即点s到点v的最短路径总是小于或等于当前点d的路径权重.证明如下: 在正是进入复杂的图前,先 ...

  9. C++ 设计模式--模板模式、策略模式、观察者模式

    现代软件设计特征:需求频繁变化 设计模式的要点是"寻找变化点",在变化点应用设计模式,从而更好的应对需求变化. 1. Template Method 在软件构建结构中,往往他有整体 ...

  10. Docker UnicodeEncodeError: 'ascii' codec can't encode characters in position

    在容器里查询nova服务的时候字符集报错问题留档及处理方法: 1.在容器里执行nova list --all 提示 [root@stack1 region_01]# nova list --all E ...