leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses
错误解法:
"[])"就会报错,没考虑到出现')'、']'、'}'时,stack为空的情况,这种情况也无法匹配
class Solution {
public:
bool isValid(string s) {
if(s.empty())
return false;
stack<char> st;
st.push(s[]);
for(int i = ;i < s.size();i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
st.push(s[i]);
else if(s[i] == ')'){
if(st.top() == '(')
st.pop();
else
return false;
}
else if(s[i] == ']'){
if(st.top() == '[')
st.pop();
else
return false;
}
else if(s[i] == '}'){
if(st.top() == '{')
st.pop();
else
return false;
}
}
return st.empty() ? true : false;
}
};
正确解法:
class Solution {
public:
bool isValid(string s) {
int length = s.size();
if(length < )
return false;
stack<char> result;
for(int i = ;i < length;i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
result.push(s[i]);
else{
if(result.empty())
return false;
if(s[i] == ')' && result.top() != '(')
return false;
if(s[i] == ']' && result.top() != '[')
return false;
if(s[i] == '}' && result.top() != '{')
return false;
result.pop();
}
}
return result.empty();
}
};
32. Longest Valid Parentheses
https://www.cnblogs.com/grandyang/p/4424731.html
这个题求的是最长的连续匹配正确的符号。
匹配错误只可能是右括号')'存在时,堆中没有左括号'('进行匹配。start用来继续这个连续匹配的开始位置,只有在匹配错误的情况下,这个start才更新。
如果匹配成功后,堆中没有左括号'(',则说明从start到当前都是匹配正确了的;
注意必须用i - position.top(),可能出现这种情况'(()()',如果你使用i - position弹出的那个位置,你永远只可能获得长度为2的,不可能获得连续的长度。
class Solution {
public:
int longestValidParentheses(string s) {
int start = ,res = ;
stack<int> position;
for(int i = ;i < s.size();i++){
if(s[i] == '(')
position.push(i);
else if(s[i] == ')'){
if(position.empty())
start = i + ;
else{
position.pop();
res = position.empty() ? max(res,i - start + ) : max(res,i - position.top());
}
}
}
return res;
}
};
自己写的一个版本,更容易理解:
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> sta;
int res = ;
int left = -;
for(int i = ;i < s.size();i++){
if(s[i] == '(')
sta.push(i);
else{
if(sta.empty())
left = i;
else{
int index = sta.top();
sta.pop();
if(sta.empty())
res = max(res,i - left);
else
res = max(res,i - sta.top());
}
}
}
return res;
}
};
301. Remove Invalid Parentheses
这个题是求删除后所有合法的,并且要求删除次数必须最少。
这个题与前面两个题稍稍有点不同,这个题需要用bfs的方式,把每个位置的字符删除加入队列判断是否合法,一旦有合法的就不再进行删除操作,而是把队列中剩下的进行判断是否合法就行了。
使用了visited数组,这样防止了搜索的重复计算
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> res;
unordered_set<string> visited;
visited.insert(s);
queue<string> q;
q.push(s);
bool finished = false;
while(!q.empty()){
string tmp = q.front();
q.pop();
if(isValid(tmp)){
res.push_back(tmp);
finished = true;
}
if(finished)
continue;
for(int i = ;i < tmp.size();i++){
if(tmp[i] != '(' && tmp[i] != ')')
continue;
string t = tmp.substr(,i) + tmp.substr(i+);
if(!visited.count(t)){
visited.insert(t);
q.push(t);
}
}
}
return res;
}
bool isValid(string s){
int count = ;
for(int i = ;i < s.size();i++){
if(s[i] != '(' && s[i] != ')')
continue;
else if(s[i] == '(')
count++;
else{
if(count <= )
return false;
else
count--;
}
}
return count == ;
}
};
leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、的更多相关文章
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 在Web.config或App.config中的添加自定义配置 <转>
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...
- ELK日志分析平台系统windows环境搭建和基本使用
ELK(ElasticSearch, Logstash, Kibana),三者组合在一起就可以搭建实时的日志分析平台啦! Logstash主要用来收集.过滤日志信息并将其存储,所以主要用来提供信息. ...
- Bell(hdu4767+矩阵+中国剩余定理+bell数+Stirling数+欧几里德)
Bell Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 最新版本elasticsearch本地搭建入门篇
最新版本elasticsearch本地搭建入门篇 项目介绍 最近工作用到elasticsearch,主要是用于网站搜索,和应用搜索. 工欲善其事,必先利其器. 自己开始关注elasticsearch, ...
- 教你分分钟搞定Python之Flask框架
用最短的时间开发一个数据操作接口,Python是王道! 一.安装pip .首先检查linux有没有安装python-pip包,终端执行 pip -V [root@ network-scripts]# ...
- JAVA的高并发基础认知 二
一.JAVA高级并发 1.5JDK之后引入高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的,充分利用了现代多处理器和多核心系统的功能以编写大规模并发 ...
- 外边距塌陷 margin collapsing
块的顶部外边距和底部外边距有时被组合(折叠)为单个外边距,其大小是组合到其中的最大外边距, 这种行为称为外边距塌陷(margin collapsing),有的地方翻译为外边距合并. 1.相邻的兄弟姐妹 ...
- twindows下omcat8安装后,不能启动服务
原因可能是cmd安装时,不是以管理员的身份运行cmd命令的.解决办法,以管理员身份运行cmd,进入tomcat安装/解压的bin目录下,先执行 service.bat remove 命令卸载服务,之后 ...
- Apex 的 Trigger 类简介
Apex Triggers Apex 触发器(Apex Triggers)是一种特殊的 Apex 类.它的主要作用是在一条记录被插入.修改.删除之前或之后自动执行一系列的操作.每一个 Trigger ...
- openvswitch技术总结
OVS技术总结 一.OVS的组成 二.OVS的匹配条件和动作 三.OVS的发展方向 四.OVS的操作实践 OVS与Namespace配合模拟租户之间的数据通信 基本思路: Namespace模拟出不同 ...