LeetCode --- Valid Parentheses
题目链接
Problem discription
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.
Accpted Code:
1 class Solution { 2 public: 3 bool isValid(string s) { 4 map<char, char> brackets; 5 brackets['{'] = '}'; 6 brackets['['] = ']'; 7 brackets['('] = ')'; 8 // use a stack to store every left brackets 9 stack<char> leftBracket; 10 // length of s 11 int len = (int)s.length(); 12 13 for (int i = 0; i < len; i++) { 14 if (s[i] == '(' || s[i] == '[' || s[i] == '{') { 15 leftBracket.push(s[i]); 16 } else { 17 // there is no left bracket 18 if (leftBracket.empty()) return false; 19 char last = leftBracket.top(); 20 // the previous left bracket doesn't match s[i] 21 if (brackets[last] != s[i]) return false; 22 leftBracket.pop(); 23 } 24 } 25 // if there are some brackets left 26 // not used(no right brackets to match them) 27 return leftBracket.empty(); 28 } 29 };
转载于:https://www.cnblogs.com/Stomach-ache/p/3783608.html
LeetCode --- Valid Parentheses相关推荐
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- LeetCode : Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 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 '(', ')', ...
最新文章
- sentinel使用
- JBPM深入解析之变量设计
- CentOS7.9关闭SELinux
- mysql 5.1.73路径_centos7.2 环境下 mysql-5.1.73 安装配置
- 前端学习(2960):实现发送axios请求
- 简要安装FreeBSD 6.2及配置桌面环境[zz]
- 将科学计数法的数值转化为字符
- 2022年顺顺顺,送3本技术好书借你千里风
- MongoDB小结26 - 地理空间索引
- java真题_2017年JAVA考试试题及答案
- CodeRunner激活
- 洛谷P3369 【模板】普通平衡树 红黑树实现
- html中取颜色快捷键,ps中填充颜色的快捷键是什么?
- 大小写英文字母对应的ASCII值
- router传参接参(详细)
- 百度CEO李彦宏:电子商务平台将在年前发布
- 询盘获客系统为什么会这么火,你知道吗?
- 分享数据时代总结思维导图模板
- 反射(com on com on gogogo!!!)
- 将以逗号隔开string字符串,转为list
热门文章
- TemplateBinding与Binding区别,以及WPF自定义控件开发的遭遇
- 给初学者的 RxJava2.0 教程 (八)
- 深入学习http协议(转)
- Mysql不同存储引擎的表转换方法
- locate: database too small: /var/db/locate.databas
- 密码可逆不可逆选择_膝关节损伤不可逆!跑步要注意!
- 记录一次webpack3升级到webpack4过程
- vmware 克隆后Linux没有eth网卡只有lo
- 1Android系统移植与驱动开发概述
- Java的LockSupport.park()实现分析