LeetCode : Valid Parentheses
问题:
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
C++参考代码:
class Solution {
public:
bool isValid(string s) {
unordered_map<char,int> m;
vector<int> v;
m['('] = 1;
m[')'] = -1;
m['{'] = 2;
m['{'] = -2;
m['{'] = 3;
m['{'] = -3;
for(int i=0;i<s.length();i++){
if(m[s[i]]>0){
v.push_back(m[s[i]]);
}else if(!v.empty() &&( m[s[i]] + v[v.size()-1] )== 0){
v.pop_back();
}else{
return false;
}
}
if(v.empty()){
return true;
}else {
return false;
}
}
};
转载于:https://www.cnblogs.com/wzy-coding/p/9676224.html
LeetCode : Valid Parentheses相关推荐
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- LeetCode --- Valid Parentheses
题目链接 Problem discription Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- LeetCode Valid Parentheses(判断括号是否匹配)
题意:给出(,[,{,),],}括号组成的字符串, 判断括号是否匹配 思路:用栈结构判断 1.如果是(,[,{,将其入栈 2.如果遇到),],},看栈是否为空.栈顶元素是否对应为(,[,{ 3.将字符 ...
- Leetcode: Valid Parentheses
与 POJ 上那道括号匹配相比, 这道可谓简单 思路: 堆栈存储符号, 遇到匹配弹出 代码: #include <iostream> #include <stack> usin ...
- LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)
Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- LeetCode 32. Longest Valid Parentheses
问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...
- LeetCode算法入门- Longest Valid Parentheses -day12
LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...
- LeetCode算法入门- Valid Parentheses -day11
LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...
最新文章
- SystemTap了解
- SQLSever 存储过程创建
- linux oracle12c dbca,Linux下Oracle 12c R2图形化安装笔记
- throws关键字_异常处理的第一种方式,交给别人处理
- php 正则提取日期,PHP正则匹配日期和时间(时间戳转换)的实例代码
- 学python用什么软件-初学 Python 需要安装哪些软件?
- Fiddler 手机抓包详解
- 浅谈股价预测模型:全能大明星——神经网络模型
- 如何修改wifi密码(wifi密码怎么修改在手机上怎么修改)
- java poi xml转word文档_使用POI转换word doc文件
- 2019年阿里最新java编程规范-在线版
- 反恐精英online单机版有各种武器
- 基于Bootstrap的响应式网页
- 计算机发展史的十大成就,我国在自然科学领域的十大成就都有哪些?
- 超微服务器只显示raid0,超微主板怎么创建RAID磁盘阵列 服务器组建RAID0、RAID1图文教程...
- MBR20100FCT低压降肖特基二极管ASEMI原装
- Livezilla安装配置, APE(Ajax Push Engine)安装配置
- cenos7开启SMB文件共享,小米摄像头无法搜索到的原因
- ngx-datatable的使用
- pandas 的基本使用
热门文章
- for vue 一行2列_awk 处理文本:行转列,列转行
- c语言中定义结构体指针的作用,c语言定义结构体指针
- 机器学习(MACHINE LEARNING)交叉验证(简单交叉验证、k折交叉验证、留一法)
- python【蓝桥杯vip练习题库】ADV-100第二大整数
- python微信自动机器人
- php获取图高度,PHP imagesx()、imagesy() – 获取图像宽度与高度_程序员人生
- $dbms=mysql_MySQL学习笔记(一)
- dedecms 如何调用多个不同栏目的文章_SEO排名,同一篇文章,在多个栏目,如何处理?...
- (5.2) Tomcat 8 源码, 初始化组件
- java 注解生成实例_Java 8 开发的 Mybatis 注解代码生成工具