原题链接在这里: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的更多相关文章

  1. [LeetCode] 301. Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  3. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  4. [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 ...

  5. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

  6. 【leetcode】301. Remove Invalid Parentheses

    题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...

  7. 301 Remove Invalid Parentheses 删除无效的括号

    删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...

  8. Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses)

    Leetcode之深度优先搜索(DFS)专题-301. 删除无效的括号(Remove Invalid Parentheses) 删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果. 说明 ...

  9. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

随机推荐

  1. ElasticSearch中碰到的C10K问题

    Elasticsearch基于Netty解决C10K问题背后的原理是JAVA NIO中的IO多路复用机制,涉及到三大"组件":SelectableChannel.Selector. ...

  2. bootstrap table--面相配置、hook、适配的表格框架

    bootstrap table--面相配置.hook.适配的表格框架

  3. 关于visual studio 2015 智能提示英文,而非中文的解决方案

    关于visual studio 2015 智能提示英文,而非中文的解决方案:   找到这个目录 C:\Program Files (x86)\Reference Assemblies\Microsof ...

  4. pycharm_python_flask相关学习心得逐步更新

    2019-10-30: Pycharm的interpreter配置问题对于安装第三方库,如果能够在配置的可视化界面安装成功更好.如果不能可视化安装,则在pycharm的terri..仿cmd下用pip ...

  5. MVC拦截

    1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Web; 5using Syst ...

  6. keras学习入门一

    基本概念 1. 张量 tensor 所有的数据类型都可以看成是张量,可以看成是向量,矩阵在推广 张量的阶,有时候也叫维度,或是轴(axis) 0阶张量如 [] ,5 也叫做标量 1阶张量 如 [ 1, ...

  7. 总结HTML5新增的标签及功能

    https://my.oschina.net/chengkuan/blog/422306 标记意义及用法分析/示例 属性/属性值/描述 <article> 定义独立的内容,如论坛帖子.报纸 ...

  8. 英语bitellos钻石bitellos单词

    大颗粒的钻石叫做bitellos,四大钻石指的就是“摄政王”.“南非之星”.“蓝色希望”和“光明之山”四颗钻石.经过琢磨的钻石光彩夺目.灿烂无比,历来被誉为“宝石之王”,科研领域里大颗粒的钻石叫做bi ...

  9. Docker 0x02: Docker生态

    目录 Docker生态 Docker官网 0x00 网址 Docker组件 0x01. docker-client 与 docker-daemon 0x02. docker镜像 0x03. docke ...

  10. 【转载】深入剖析自定义View之onMeasure

    1.前言 自定义View中我们看到很多都重写了onMeasure方法,那么我们首先得知道onMeasure是做什么的.onMeasure中文意思就是测量,所以它是用于测量View的大小,影响View大 ...