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.
这一题是典型的使用压栈的方式解决的问题,题目中还有一种valid情况没有说明,需要我们自己考虑的,就是"({[]})"这种层层嵌套但
可以完全匹配的,也是valid的一种。解题思路是这样的:我们对字符串S中的每一个字符C,如果C不是右括号,就压入栈stack中。
如果C是右括号,判断stack是不是空的,空则说明没有左括号,直接返回not valid,非空就取出栈顶的字符pre来对比,如果是匹配
的,就弹出栈顶的字符,继续取S中的下一个字符;如果不匹配,说明不是valid的,直接返回。当我们遍历了一次字符串S后,注意
这里还有一种情况,就是stack中还有残留的字符没有得到匹配,即此时stack不是空的,这时说明S不是valid的,因为只要valid,一
定全都可以得到匹配使左括号弹出。
class Solution {
public:
bool isValid(string s) {
if(s.size()%2!=0)
return false;
stack<int> stk;
int len=s.size(),i;
for(i=0;i<len;i++)
{
if(s[i]==')'||s[i]==']'||s[i]=='}')
{
if(!stk.empty())
{
char c = stk.top();
if ((c == '(' && s[i] != ')') || (c == '[' && s[i] != ']') || (c == '{' && s[i] != '}'))
return false;
else
stk.pop();
}
else
return false;
}
else
stk.push(s[i]); }
return stk.empty(); }
};
这一题是典型的使用压栈的方式解决的问题,题目中还有一种valid情况没有说明,需要我们自己考虑的,就是"({[]})"这种层层嵌套但
可以完全匹配的,也是valid的一种。解题思路是这样的:我们对字符串S中的每一个字符C,如果C不是右括号,就压入栈stack中。
如果C是右括号,判断stack是不是空的,空则说明没有左括号,直接返回not valid,非空就取出栈顶的字符pre来对比,如果是匹配
的,就弹出栈顶的字符,继续取S中的下一个字符;如果不匹配,说明不是valid的,直接返回。当我们遍历了一次字符串S后,注意
这里还有一种情况,就是stack中还有残留的字符没有得到匹配,即此时stack不是空的,这时说明S不是valid的,因为只要valid,一
定全都可以得到匹配使左括号弹出。
Valid Parentheses——栈经典的更多相关文章
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 46.Valid Parentheses
Valid Parentheses My Submissions QuestionEditorial Solution Total Accepted: 106346 Total Submissions ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
随机推荐
- move_base代码学习一
System overview move_base 源码 API nav_core BaseGlobalPlanner BaseLocalPlanner RecoveryBehavior Recove ...
- 什么是end-to-end神经网络?
https://www.zhihu.com/question/51435499 来源:知乎著作权归作者所有. 国立台湾大学的李宏毅教授在其机器学习课程中有讲到深度神经网络的 End-to-end Le ...
- Qt实现截屏并保存(转载)
原博地址:http://blog.csdn.net/qinchunwuhui/article/details/52869451?_t_t_t=0.28889142944202306 目前对应用实现截屏 ...
- CDOJ--1237
原体连接:http://acm.uestc.edu.cn/problem.php?pid=1237 分析:质因子单增:在寻找下一个质因子时,从前一个开始. #include<iostream&g ...
- bzoj3938 Robot
3938: Robot Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 336 Solved: 112[Submit][Status][Discuss ...
- 什么是JavaFX
什么是JavaFX JavaFx平台是一个富客户端平台解决方案,它能够使用应用程序开发人员轻松的创建跨平台的富客户端应用程序.它构建在Java技术的基础之上,JavaFX平台提供了一组丰富的图形和媒体 ...
- 将new Date() 格式化为 ’2018-10-11‘ 的字符串格式
function dateToString( date , format ){ if(!date) return ""; if (!Common.type.isDate(date) ...
- 复制自身程序到windows目录和system32目录下
功能:复制自身到windows目录和system32目录下. 参考代码: #include <stdio.h> #include <windows.h> void CopySe ...
- sklearn中的model_selection模块(1)
sklearn作为Python的强大机器学习包,model_selection模块是其重要的一个模块: 1.model_selection.cross_validation: (1)分数,和交叉验证分 ...
- Redis-1-Redis的安装
Redis 什么是Redis? redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value数据库. 安装Redis: windows下如何安装? 官方网址:ht ...