301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果。
注意: 输入可能包含了除 ( 和 ) 以外的元素。
示例 :
"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]
详见:https://leetcode.com/problems/remove-invalid-parentheses/description/
方法一:
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> ans;
helperDFS(s, ')', 0,ans);
return ans;
}
void helperDFS(string s, char ch, int last,vector<string> &ans)
{
for(int i = 0, cnt = 0; i < s.size(); i++)
{
if(s[i]=='('||s[i]==')')
{
s[i]==ch?cnt++:cnt--;
}
if(cnt <= 0)
{
continue;
}
for(int j = last; j <= i; j++)
{
if(s[j] == ch && (j ==last || s[j-1]!= ch))
{
helperDFS(s.substr(0, j)+s.substr(j+1), ch, j,ans);
}
}
return;
}
reverse(s.begin(), s.end());
if(ch == ')')
{
return helperDFS(s, '(', 0,ans);
}
ans.push_back(s);
}
};
方法二:
class Solution {
public:
vector<string> removeInvalidParentheses(string s) {
vector<string> res;
unordered_set<string> visited{{s}};
queue<string> q{{s}};
bool found = false;
while (!q.empty())
{
string t = q.front();
q.pop();
if (isValid(t))
{
res.push_back(t);
found = true;
}
if (found)
{
continue;
}
for (int i = 0; i < t.size(); ++i)
{
if (t[i] != '(' && t[i] != ')')
{
continue;
}
string str = t.substr(0, i) + t.substr(i + 1);
if (!visited.count(str))
{
q.push(str);
visited.insert(str);
}
}
}
return res;
}
bool isValid(string t) {
int cnt = 0;
for (int i = 0; i < t.size(); ++i)
{
if (t[i] == '(')
{
++cnt;
}
else if (t[i] == ')' && --cnt < 0)
{
return false;
}
}
return cnt == 0;
}
};
参考:https://blog.csdn.net/qq508618087/article/details/50408894
https://www.cnblogs.com/grandyang/p/4944875.html
301 Remove Invalid Parentheses 删除无效的括号的更多相关文章
- [leetcode]301. Remove Invalid Parentheses 去除无效括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] 301. Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses去除不符合匹配规则的括号
[抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- Leetcode 301.删除无效的括号
删除无效的括号 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明: 输入可能包含了除 ( 和 ) 以外的字符. 示例 1: 输入: "()())()" 输出 ...
随机推荐
- hdu - 1104 Remainder (bfs + 数论)
http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...
- 解析excel文件并将数据导入到数据库中
今天领导给安排了一个临时工作,让我将一个excel里面的数据解析后放入数据库中,经过一个下午的努力成功完成,现在将代码献上,希望对大家有所帮助 一.需要导入的jar 1.commons-collect ...
- 携程Apollo(阿波罗)配置中心把现有项目的配置文件迁移到Apollo
说明: 1.这个示例应该算是一个静态迁移,也就是说配置更新后要重启应用才能体现更新,目的是展示现有配置的如何迁移. 2.如果要实现更新配置后动态去更新而不重启应用的操作,比如ZK地址和数据库地址这些, ...
- 8VC Venture Cup 2016 - Final Round (Div2) E
贪心.当前位置满油可达的gas station中,如果有比它小的,则加油至第一个比他小的.没有,则加满油,先到达这些station中最小的.注意数的范围即可. #include <iostrea ...
- Django学习系列之中间件
中间件的定义 中间件是一个.一个的管道,如果相对任何所有的通过Django的请求进行管理都需要自定义中间件 中间件可以对进来的请求和出去的请求进行控制 中间件是一类 django请求生命周期 自定义中 ...
- Linux挂载新盘
Linux 系统挂载数据盘 1.查看数据盘 使用“fdisk-l”命令查看 2. 对数据盘进行分区 执行“fdisk /dev/sdb”命令,对数据盘进行分区: 输入“n”,“p”“1”,两次回车,“ ...
- 【Unix编程】进程间通信(IPC)
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息.IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket. ...
- Django打造大型企业官网(二)
三.项目环境搭建 3.1.创建项目环境和安装包 创建django项目 mkvirtualenv DjangoProject workon DjangoProject pip install -i ht ...
- Mariadb 主从
一 mariadb主从多用于网站架构,因为该主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构 ...
- Hibernate学习笔记(六) — Hibernate的二级缓存
我们知道hibernate的一级缓存是将数据缓存到了session中从而降低与数据库的交互.那么二级缓存呢? 一.应用场合 比方.在12306购票时.须要选择出发地与目的地,假设每点一次都与数据库交互 ...