[抄题]:

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 ).

Example 1:

Input: "()())()"
Output: ["()()()", "(())()"]

Example 2:

Input: "(a)())()"
Output: ["(a)()()", "(a())()"]

Example 3:

Input: ")("
Output: [""]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

一看“匹配”就以为要用stack:果然很麻烦

用bfs: 随着index i 的增加,括号元素可以加也可以不加 在sb.append中体现,所以用dfs

[英文数据结构或算法,为什么不用别的数据结构或算法]:

hashset:dfs是随机的 可能出现重复现象,所以用来给结果去重

dfs的格式:

dfs(起点变量,终点变量,过程变量)

[一句话思路]:

为了去除的括号数量最少,只要左右相等就可以 所以用L R来控制dfs递增的次数

只能右消左(),不能左消右)(。因为是反的。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 主函数中调用R L要带入字母,不是0
  2. DFS中不需要写for,每次只对一个变量进行处理

[二刷]:

  1. i == length必定会退出,指标为0才添加到答案中
  1. 刚新建就被返回了,此时可以return 二合一

[三刷]:

  1. DFS的顺序是左括号先不用,再用 先按照初始值来。虽然不知道为什么。

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

选择括号要不要可以用DFS

[复杂度]:Time complexity: O(2^n) Space complexity: O(n)

[算法思想:递归/分治/贪心]:递归

[关键模板化代码]:

[其他解法]:

BFS可是慢啊

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public List<String> removeInvalidParentheses(String s) {
//ini: count L, R. Set, List<String>
int L = 0, R = 0, n = s.length();
Set<String> set = new HashSet<>();
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '(') L++;
else if (s.charAt(i) == ')') {
if (L > 0) L--;
else R++;
}
} System.out.println("L =" + L);
System.out.println("R =" + R); //dfs
dfs(s, 0, set, new StringBuilder(), L, R, 0); //return res
return new ArrayList<String>(set);
} public void dfs(String s, int i, Set<String> set, StringBuilder sb, int L, int R, int open) {
//cc: exit
if (L < 0 || R < 0 || open < 0) return ; //normal return
if (i == s.length()) {
if (L == 0 && R == 0 && open == 0) set.add(sb.toString());
return ;
} //getlen of sb
int len = sb.length();
char c = s.charAt(i); if (c == '(') {
//not use (
dfs(s, i + 1, set, sb, L - 1, R, open);
//use (
dfs(s, i + 1, set, sb.append(c), L, R, open + 1);
}else if (c == ')') {
//not use )
dfs(s, i + 1, set, sb, L, R - 1, open);
//use )
dfs(s, i + 1, set, sb.append(c), L, R, open - 1);
}else {
dfs(s, i + 1, set, sb.append(c), L, R, open);
}
//set len for sb
sb.setLength(len); } }

[是否头一次写此类driver funcion的代码] :

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

    原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...

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

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

  6. 【leetcode】301. Remove Invalid Parentheses

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

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

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

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

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

  9. Remove Invalid Parentheses

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

随机推荐

  1. win7 QT +opencv环境搭建

    1.Win7 Qt4.8.5+QtCreator2.8.0+mingw环境参考前博文先搭建 2.下载Cmake2.8.11.2版本,安装.运行 [项目]那编译器选择:MinGW4.4.另外,重新编译O ...

  2. CentOS部署NetCore - 3. 部署站点

    一. CentOS 安装FTP linux 安装 ftp第一步 使用如下命令#rpm -qa |grep vsftpd可以检测出是否安装了vsftpd软件, 如果没有安装,使用YUM命令进行安装. y ...

  3. JDK 8 - Method Reference 分析

    Java SE 8 在 Java 语言层面上新增了 lambda expression 的功能,使得 Java 具备了函数式语言的能力 - 可以将函数作为方法参数传递,即 code as data. ...

  4. 20165226 MySort的实现

    MySort的实现 一.实验要求 研究sort的其他功能,要能改的动代码,模拟实现Linux下Sort -t : -k 2的功能. 二.代码 /** * Created by xiang on 201 ...

  5. canvas之画一条线段

    var canvas=document.getElementById("canvas"); //设置绘图环境 var cxt=canvas.getContext('2d'); // ...

  6. Druid.io系列(二):基本概念与架构

    原文链接:  https://blog.csdn.net/njpjsoftdev/article/details/52955788 在介绍Druid架构之前,我们先结合有关OLAP的基本原理来理解Dr ...

  7. 转 maven jetty 插件

    maven jetty 插件使用 本机环境 JDK 7 Maven 3.2 Jetty 9.2 Eclipse Luna pom.xml 配置 在你的 pom.xml 文件中添加 jetty 插件的描 ...

  8. python json.dumps() 中文乱码问题

    python json.dumps() 中文乱码问题   python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...

  9. svg_png

    #!/usr/bin/env python#-*- encoding=UTF-8 -*-from __future__ import print_functionimport sysimport ra ...

  10. 超简单让ubuntu开启wifi热点(亲测16.04与14.04可用)

    今天教大家一个简单方法让ubuntu发散wifi热点给手机或者其他设备使用. 首先,创建一个普通的热点,点击右上角的网络,然后选择下拉菜单中的编辑连接,然后出现以下界面. 然后点击增加,连接类型选接W ...