LeetCode 020 Valid Parentheses
题目描述: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的更多相关文章
- 【JAVA、C++】LeetCode 020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode][020] Valid Parentheses (Java)
题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
随机推荐
- Java创建二叉树、二叉树的遍历
创建二叉树: public class Node { // 左子节点 public Node leftNode; // 右子节点 public Node rightNo ...
- AQS源码深入分析之独占模式-ReentrantLock锁特性详解
本文基于JDK-8u261源码分析 相信大部分人知道AQS是因为ReentrantLock,ReentrantLock的底层是使用AQS来实现的.还有一部分人知道共享锁(Semaphore/Count ...
- Day13 CSS样式
1. css 选择器 1.1 css三种引入方法 <!DOCTYPE HTML> <html> <head> <meta charset="utf- ...
- solr 文档一
[在此处输入文章标题] 参考博客: http://blog.csdn.net/matthewei6/article/details/50620600 基础环境搭建 solr版本5.5.5: 一.sol ...
- 白话科普系列——最好用的浏览器 Chrome,你用了么?
市面上的浏览器多种多样,而浏览器的王者--IE 浏览器,它在 2002 年市场份额高达 95.4%.直到后续 Firefox,Safari,Chrome 相继问世才动摇了 IE 浏览器的地位,其中 C ...
- ELF文件格式内容
在计算机科学中,是一种用于二进制文件.可执行文件.目标代码.共享库和核心转储格式文件. ELF文件组成部分 ELF文件由4部分组成,分别是ELF头(ELF header).程序头表(Program ...
- FreeSql接入CAP的实践
CAP CAP 是一个基于 .NET Standard 的 C# 库,它是一种处理分布式事务的解决方案,同样具有 EventBus 的功能,它具有轻量级.易使用.高性能等特点. https://git ...
- SSY的队列 hash+记忆化
题目描述 \(SSY\) 是班集体育委员,总喜欢把班级同学排成各种奇怪的队形,现在班级里有 \(N\) 个身高互不相同的同学,请你求出这 \(N\) 个人的所有排列中任意两个相邻同学的身高差均不为给定 ...
- 七:Redis的持久化
1.RDB(Redis DataBase) 1.1 定义:在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的snapshot快照,他恢复时是将快照文件直接读到内存里 是什么:Redis会单 ...
- cephonebox发布
前言 现在已经是2016年收官的一个月了,之前一直想做一个calamari的集成版本,之所以有这个想法,是因为,即使在已经打好包的情况下,因为各种软件版本的原因,造成很多人无法配置成功,calamar ...