leetcode解题报告(7):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.
分析
遇到这种括号匹配的问题,当然要用栈来写。先把string的第一个元素压入栈,再从第二个元素开始遍历,如果栈顶元素为和当前string的元素相匹配,就把该元素出栈。如果在遍历期间堆栈为空了,比如输入的是:
[]{}()
那么在i=1的时候栈就会为空,这时候取栈顶元素是没有意义的。因此如果遍历期间发现栈为空,就直接把当前的string元素压入栈中。
另外,如果输入的string的长度不是偶数,那么肯定是不匹配的,在一开始的时候做这个判断,可以筛掉很多种不匹配的情况。
总之,如果输入的string是匹配的,那么最终的栈一定为空。
为了判断栈顶元素和当前string的元素是否相匹配,可以定义一个名为isOK的函数。
代码如下:
class Solution {
public:
bool isOK(const char &a,const char &b){
if((a == '(' && b == ')')
|| (a == '[' && b == ']')
|| (a == '{' && b == '}'))
return true;
else
return false;
}
bool isValid(string s) {
if(s.size() % 2 != 0)return false;
stack<char>sc;
sc.push(s[0]);
for(int i = 1; i != s.size(); ++i){
if(sc.empty())
sc.push(s[i]);
else{
if(isOK(sc.top(),s[i]))
sc.pop();
else
sc.push(s[i]);
}
}
return sc.empty();
}
};
下面是另一种更优雅的解法:
https://leetcode.com/problems/valid-parentheses/#/solutions
class Solution {
public:
bool isValid(string s) {
if(s.size() % 2 != 0)return false;
stack<char>paren;
for(char& c : s){
switch(c){
case '(':
case '[':
case '{':paren.push(c);break;
case ')':if(paren.empty() || paren.top() != '(')return false;paren.pop();break;
case ']':if(paren.empty() || paren.top() != '[')return false;paren.pop();break;
case '}':if(paren.empty() || paren.top() != '{')return false;paren.pop();break;
default:;
}
}
return paren.empty();
}
};
leetcode解题报告(7):Valid Parentheses的更多相关文章
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
随机推荐
- spring cloud微服务实践四
spring cloud的hystrix还有一个配搭的库hystrix-dashboard,它是hystrix的一款监控工具,能直观的显示hystrix响应信息,请求成功率等.但是hystrix-da ...
- prometheus+grafana+Alertmanager邮箱告警
环境 系统:CentOS 7 软件:alertmanager-0.18.0.linux-amd64.tar.gz 安装 下载二进制包 地址:https://prometheus.io/download ...
- 正则表达式的运行原理详解(NFA,多分支原理)
原文:https://blog.csdn.net/yx0628/article/details/82722166
- Spring邮件服务:Maven + Spring SMTP Mail (可发送附件)
1 spring-smtp-mail.xml <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- 总结一下C++与C#之间的区别
1,C#不支持多重继承 2.在标准的C#安全代码中不支持指针类型的操作,然而,你却能在微软所谓的“非安全代码”中操作指针类型对象. 3.C#中所有对象都只能通过关键词“new”来创建,C++的“类名_ ...
- robot framework 如何获取隐藏元素的文本,以及可见元素的文本
1.下图是获取可见元素的文本内容,运行后得到:${B_name}=公告管理:假设公告管理不可见,那么${B_name}=‘’(为空)
- multer实现图片上传
multer实现图片上传: ejs代码: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- Android开发中常见问题分析及解决
最近公司有新的业务需求,需要开发一款APP,因为我开发过Android APP(我想告诉他们,那是4年前的事了,嘤嘤嘤),就把开发任务交给我了,当然也不是我一个人啦,让我组开发小组,说白了,就是让我来 ...
- JS密码强度检测
//校验密码强度---沒有匹配到以下級別就提示 function checkPassWord(value){ // 0: 表示第一个级别 1:表示第二个级别 2:表示第三个级别 // 3: 表示第四个 ...
- shell取消键盘回显
使用下面这个命令取消回显 stty -echo 使用下面这个命令打开回显 stty echo