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学习的第三十九天
1.例3.7 100~200之间全部素数 package bgio; public class cjava { public static void main(String[]args) { int ...
- 【Azure 环境】存储在Azure上的文件,使用IE/Edge时自动打开的问题,如何变为下载而非自动打开
问题描述 存储,作为云服务最重要的一部分.当需要从云存储中下载文件时,时常面临一些格式的文件被浏览器自动打开而非下载,那如何来解决这个问题呢? 在Azure中,存储的服务有以下方式: Azure Bl ...
- 红帽6.9搭建yum源的2种方式(HTTP和本地)
方式一:HTTP搭建 1.首先删除本身所带的yum `rpm -qa | grep yum | xargs rpm -e --nodeps ` #忽略依赖关系,强行删除 若出现 错误出现 将后面的 ...
- 自动化测试之Selenium篇(一):环境搭建
当前无论找工作或者是实际项目应用,自动化测试扮演着非常重要的角色,今天我们来学习下Selenium的环境搭建 Selenium简述 Selenium是一个强大的开源Web功能测试工具系列 可进行读入测 ...
- C# OpenFileDialog和SaveFileDialog的常见用法
#region 示例1 SaveFileDialog sfd = new SaveFileDialog(); //设置文件类型 sfd.Filter = "备份文件(*.bak)|*.bak ...
- leetcode144 longest-palindromic-substring
题目描述 找出给出的字符串S中最长的回文子串.假设S的最大长度为1000,并且只存在唯一解. Given a string S, find the longest palindromic substr ...
- 【阿里云-大数据】阿里云DataWorks学习视频汇总
阿里云DataWorks学习视频汇总 注意:本文档中引用的视频均来自阿里云官方的帮助文档,本文档仅仅是汇总整理,方便学习. 阿里云DataWorks帮助文档链接:https://help.aliyun ...
- Android基础——项目的文件结构(一)
Android基础--项目的文件结构(一) Android视图与Project视图对比 [注]此项目文件结构仅限于Android Studio下的Android项目!!! 在Android Studi ...
- Pytest学习(六) - conftest.py结合接口自动化的举例使用
一.conftest.py作用 可以理解成存放fixture的配置文件 二.conftest.py配置fixture注意事项 pytest会默认读取conftest.py里面的所有fixture co ...
- Hopfield Network 霍普菲尔德网络入门
简介 Hopfield Network (霍普菲尔德网络),是 Hopfield 在1982年提出的一种基于能量的模型,发表的文章是 Neural networks and physical syst ...