301. 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 ).
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递增的次数
只能右消左(),不能左消右)(。因为是反的。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 主函数中调用R L要带入字母,不是0
- DFS中不需要写for,每次只对一个变量进行处理
[二刷]:
- i == length必定会退出,指标为0才添加到答案中
- 刚新建就被返回了,此时可以return 二合一
[三刷]:
- 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去除不符合匹配规则的括号的更多相关文章
- [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
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- 301 Remove Invalid Parentheses 删除无效的括号
删除最小数目的无效括号,使输入的字符串有效,返回所有可能的结果.注意: 输入可能包含了除 ( 和 ) 以外的元素.示例 :"()())()" -> ["()()() ...
- 【leetcode】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 ...
- Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- Rabbitmq交换器Exchange和消息队列
通常我们谈到队列服务, 会有三个概念: 发消息者.队列.收消息者,RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者 ...
- phpMyAdmin“缺少 mcrypt 扩展。请检查 PHP 配置。”解决办法
在ecmall二次开发中因php版本要求低于5.3,而如下更新要求升级PHP,所以以下方式不适合于ecmall商城项目. 解决办法:安装php-mcrypt libmcrypt libmcrypt-d ...
- 如何将angular-ui-bootstrap的图片轮播组件封装成一个指令
在项目开发中我们经常会遇到图片轮播的功能点: 如果我们开发人员自己原生手写,将会花费很多的时间,最终得不偿失. 接下来就详细说说如何使用angular-ui发热图片轮播模块,并且将它写成一个指令(便于 ...
- jeecg选择按钮带入其他单据值
前端的标签 <input class="inputxt" id="fshimian" name="fshimian" ignore=& ...
- CMD中文显示为乱码
中文显示为乱码 临时解决方案: 在 CMD 中运行 chcp 936. 永久解决方案: 打开不正常的 CMD 或命令提示符窗口后,单击窗口左上角的图标,选择弹出的菜单中的“默认值”,打开如下图的对话框 ...
- Hibernate学习3—映射对象标识符(OID)
一.Hibernate 用对象标识符(OID)来区分对象 作如下代码的实验: public class StudentTest { public static void main(String[] a ...
- 数据结构与算法JavaScript描述——栈
栈就是和列表类似的一种数据结构,它可用来解决计算机世界里的很多问题. 栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样的操作很快,而且容易实现. 栈的使用遍布程序语言实现的方方面面,从表 ...
- 1006 Sign In and Sign Out (25)(25 分)思路:普通的时间比较题。。。
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
- python多分类预测模版,输出支持度,多种分类器,str的csv转float
预测结果为1到11中的1个 首先加载数据,训练数据,训练标签,预测数据,预测标签: if __name__=="__main__": importTrainContentdata( ...
- Windows2012使用笔记
一.介绍 win 2012的名字于北京时间2012年4月18日公布,全称Windows Server 2012(下面简称win 2012),正式版于2012年9月4日发布.这是一套基于Windows ...