LeetCode 301. Remove Invalid Parentheses
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/
题目:
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
Note: The input string may contain letters other than the parentheses (
and )
.
Examples:
"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]
题解:
题目要求减少最小个数的非法括号,要求最小,就想到BFS.
Queue 里加入 原来的string s, 循环中从queue中poll出来一个string. 每次减掉一个括号,若是合法了就加到res中,同时停止继续enqueue, 只把queue中现有的一个level检查了就好。
若是没遇到合法的,就把str从头到尾减掉一个括号放回到queue中。
检查是否合法用isValid. 这种写法不需要stack, 节省了空间。
同时使用Set 来记录已经检查过的string, 检查过就不需要再次检查。
Time Complexity: exponential. n = s.length(). Space: O(n).
AC Java:
public class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> res = new ArrayList<String>();
if(s == null){
return res;
} LinkedList<String> que = new LinkedList<String>();
HashSet<String> visited = new HashSet<String>(); //存储已经检查过的string, 没有必要重复检查
que.add(s);
visited.add(s);
boolean found = false; //标签,检查是否找到了valid parentheses while(!que.isEmpty()){
String str = que.poll();
if(isValid(str)){
res.add(str);
found = true;
}
if(found){ //已经找到,就在本level中找剩下的就好了
continue;
}
for(int i = 0; i<str.length(); i++){
if(str.charAt(i) != '(' && str.charAt(i) != ')'){
continue;
}
String newStr = str.substring(0,i) + str.substring(i+1);
if(!visited.contains(newStr)){
que.add(newStr);
visited.add(newStr);
}
}
}
return res;
} private boolean isValid(String s){
int count = 0;
for(int i = 0; i<s.length(); i++){
if(s.charAt(i) == '('){
count++;
}
if(s.charAt(i) == ')'){
if(count == 0){
return false;
}else{
count--;
}
}
}
return count == 0;
}
}
类似Minimum Remove to Make Valid Parentheses.
LeetCode 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. Return ...
- [LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS
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】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- 301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...
- Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)
Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- FastReport 程序员手册
一.使用TfrxReport 组件工作1.加载并存储报表默认情况下,报表窗体同项目窗体构存储在同一个DFM文件中.多数情况下,无须再操作,因而你就不必采用特殊方法加载报表.如果你决定在文件中存储报表窗 ...
- 60 网络编程(二)——URL
认识URI.URL.URN 详细请参考:https://blog.51cto.com/xoyabc/1905492 URI:uniform resource Indent 统一资源标识符 URL:un ...
- K8S学习笔记之k8s使用ceph实现动态持久化存储
0x00 概述 本文章介绍如何使用ceph为k8s提供动态申请pv的功能.ceph提供底层存储功能,cephfs方式支持k8s的pv的3种访问模式ReadWriteOnce,ReadOnlyMany ...
- 6、VUE指令
1.指令的格式 1.1. 指令的概念 指令是指带有v-前缀的特殊属性,指令的职责是当其表达式的值改变时,相应的将某些行为应用到DOM上. 1.2. 指令必须是html的属性 指令只能以带前缀的html ...
- delphi 格式转换
TO_CHAR 是把日期或数字转换为字符串 TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_NUMBER 将字符转化为数字 TO_CHAR 使用TO_CHAR函数处理数字 TO_CHA ...
- 在Unity 5中优化SkinnedMeshRenderer
过早优化是万恶之源”——Donald Knuth 不少开发者在前期开发过程中对算法等类似的开销都甚少关心,而是更倾向于尽可能简单的解决某个问题,后面必要时再进行优化.这能极大加速开发进度 ...
- delegate、Action、Func的用法
委托的特点 委托类似于 C++ 函数指针,但它们是类型安全的. 委托允许将方法作为参数进行传递. 委托可用于定义回调方法. 委托可以链接在一起. delegate的用法 delegate void B ...
- Linux环境下:vmware安装Windows报错误-无人参与应答文件包含的产品密钥无效
最近在安装window server 2012 R2的时候,输入好密钥可以继续安装,但在后面又提示我“无人参与应答文件包含的产品密钥无效.删除无效的密钥或在无人参与应答文件中提供有效的产品密钥继续进行 ...
- MongoDB(NoSQL) 非关系型数据库
目录 简单了解 mongoDB 简单使用mongoDB 简单了解 mongoDB # NoSQL 泛指非关系型的数据库 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅 ...
- Oracle数据库主外键 级联删除记录
/** * 1. NO ACTION :指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行. * * 2. SET NULL :指当删除主表中被引用列的数据时,将子表中相应引 ...