【LeetCode】020. 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.
题解:
括号匹配问题,与出现顺序有关,联想到栈和队列。
Solution 1
class Solution {
public:
bool isValid(string s) {
if (s.empty())
return true;
stack<char> st; for (int i = ; i < s.size(); ++i) {
char cur = s[i];
if (cur == '[' || cur == '(' || cur == '{') {
st.push(cur);
continue;
}
if (st.empty())
return false;
if (cur == ']' && st.top() != '['
|| cur == '}' && st.top() != '{'
|| cur == ')' && st.top() != '(')
return false;
st.pop();
} return st.empty();
}
};
【LeetCode】020. Valid Parentheses的更多相关文章
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- 【LeetCode】20. Valid Parentheses
题目:
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】65. Valid Number
Difficulty: Hard More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
随机推荐
- Web前端开发规范【HTML/JavaScript/CSS】
前言 这是一份旨在增强团队的开发协作,提高代码质量和打造开发基石的编码风格规范,其中包含了 HTML, JavaScript 和 CSS/SCSS 这几个部分.我们知道,当一个团队开始指定并实行编码规 ...
- 【BZOJ3707】圈地 几何
[BZOJ3707]圈地 Description 2维平面上有n个木桩,黄学长有一次圈地的机会并得到圈到的土地,为了体现他的高风亮节,他要使他圈到的土地面积尽量小.圈地需要圈一个至少3个点的多边形,多 ...
- PHP数组各种操作与函数汇总
对于Web编程来说,最重要的就是存取和读写数据了.存储方式可能有很多种,可以是字符串.数组.文件的形式等.数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是我学习的小结,借 ...
- Hadoop如何修改HDFS文件存储块大小
一. 临时修改可以在执行上传文件命令时,显示地指定存储的块大小.1. 查看当前 HDFS文件块大小我这里查看HDFS上的TEST目录下的jdk-7u25-linux-x64.gz 文件存储块大小.1 ...
- 每天一个Linux命令(17)whereis命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. (1)用法: 用法: w ...
- c的详细学习(9)结构体与共用体的学习(一)
C语言提供了另外两种构造类型:结构体与公用体,用来存储若干个类型不同但彼此组成一个集合的数据总体. (1)结构体类型与结构体变量 1.定义 其一般形式为: struct 结构体类型名{ 数据类型1 ...
- [原创]java WEB学习笔记26:MVC案例完整实践(part 7)---修改的设计和实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- mysql innobackupex备份实施
最近用innobackup进行备份测试,我们只备份一个innodb类型的库,数据大小大概50多G,用innobackupex大概用了5个多小时,但是mysqldump只用了大约2个小时,这让我很费解, ...
- ubuntu下搭建Scrapy框架简单办法
1. 先执行以下命令 sudo apt-get install python-lxml sudo apt-get install libxslt1-dev sudo apt-get install p ...
- 针对firefox ie6 ie7 ie8的css样式中的line-height属性
针对firefox ie6 ie7 ie8的css样式中的line-height属性 以前我们大部分都是用!important来hack,对于ie6和firefox测试可以正常显示,但是ie7以上对! ...