原题链接在这里: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. C++之救济金发放问题

    n(n<20)个人站成一圈,逆时针编号为1~n.有两个官员,A从1开始逆时针数,B从n开始顺时针数.在每一轮中,官员A数k个就停下来,官员B数m个就停下来(注意有可能两个官员停在同一个人上).接 ...

  2. .net Core MongoDB用法演示

    C#驱动MongoDB的本质是将C#的操作代码转换为mongo shell,驱动的API也比较简单明了,方法名和js shell的方法名基本都保持一致,熟悉mongo shell后学习MongoDB的 ...

  3. TCMalloc - 基本流程

    SizeMap tcmalloc通过classid将不同的小对象映射到不同的对象桶中,sizemap记录了一些对象大小和对象class的映射以及反向映射,除此之外,还记录了一些ThreadCache与 ...

  4. 绝对有效 IntelliJ IDEA2019.2下载、安装及破解教程

        原文链接:https://blog.csdn.net/weixin_43904316/article/details/88881238                   https://bl ...

  5. 使用国内作者制作的gcr.io镜像安装工具

    项目地址:https://github.com/zhangguanzhang/gcr.io How to use? 拉取 假设需要拉取gcr.io/google_containers/pause:3. ...

  6. Django(一)初始

    一:Web开发中的基本术语 1.两种架构 (1)cs架构 Client/Server:客户端-服务端架构 优点:CS能充分发挥客户端PC的处理能力,很多工作可以在客户端处理后在提交给服务器,用户体验好 ...

  7. 基于react-app搭建react-router+redux项目

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  8. 教你玩转Git-合并冲突

    Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件.Git 与 ...

  9. JavaScript 之 RegExp 对象

    RegExp 正则表达式对象 一.正则表达式 正则表达式:定义字符串的组成规则. 1.单个字符:[ ] 如:[a].[ab].[a-zA-Z0-9] 特殊符号代表特殊含义的单个字符: \d:单个数字字 ...

  10. 地产propretie单词propretie财产

    中文名:房产财产地产 外文名:property.propretie 释义:财产.所有物等 用法:作名词. 词汇搭配动词+-等 目录 1 英文释义 2 释义例句 3 词汇搭配 4 衍生 英文释义 1. ...