博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Remove Invalid Parentheses
阅读量:6271 次
发布时间:2019-06-22

本文共 3444 字,大约阅读时间需要 11 分钟。

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())()"]")(" -> [""]

DFS solution with optimizations:

  1. Before starting DFS, calculate the total numbers of opening and closing parentheses that need to be removed in the final solution, then these two numbers could be used to speed up the DFS process.
  2. Use while loop to avoid duplicate result in DFS, instead of using HashSet.
  3. Use count variable to validate the parentheses dynamically.

openN and closeN means the total numbers of opening and closing parentheses that need to be removed in the final solution (removed the minimum number of invalid parentheses).

 

diff means in the stringbuffer we build, #of '(' minus # of ')'. If at some recursion, diff<0. that's not valid expression, should directly return.

This diff here is not just for pruning, but also have the important meaning to make sure that stringbuffer is always correct. So we must have a diff in the recursion

 

sb.append(arr, cur, i);

dfs(s, cur + i, diff + i, openN, closeN, res, sb) means not to remove the current parenthesis, keep it in stringbuffer sb.

dfs(s, cur + 1, diff, openN - 1, closeN, result, sb) means to remove the current parenthesis. We won't need to do the dfs if openN has been decreased to zero - if the openN is zero, that means there are already enough open parentheses has been removed, and continually removing open parenthesis won't be a possible solution.

Watch out for duplicate. If red means keep in stringbuffer and white means remove, then the following case are duplicate: 

1. ( ( )

2. ( ( )

3. ( ( )

So our strategy is as 45 and 58 line shows: if you choose to keep one parenthese, then you have to keep all the following same parentheses

for the above cases, we only have 

1. ( ( )

2. ( )

 

1 public class Solution { 2     public List
removeInvalidParentheses(String s) { 3 List
res = new ArrayList
(); 4 if (s==null || s.length()==0) { 5 res.add(""); 6 return res; 7 } 8 int diff=0, openN=0, closeN=0; 9 char[] arr = s.toCharArray();10 for (int i=0; i
res, int cur, int diff, int openN, int closeN, StringBuffer sb) {30 if (diff < 0) return;31 if (cur == arr.length) {32 if (openN==0 && closeN==0) {33 res.add(sb.toString());34 }35 return;36 }37 if (arr[cur]!='(' && arr[cur]!=')') {38 sb.append(arr[cur]);39 dfs(arr, res, cur+1, diff, openN, closeN, sb);40 sb.deleteCharAt(sb.length()-1);41 }42 else if (arr[cur] == '(') {43 //choice one: keep this '(' in stringbuffer, but in order to avoid duplicate, has to keep all consecutive '('44 int i=0;45 while (cur+i
0) {54 dfs(arr, res, cur+1, diff, openN-1, closeN, sb);55 }56 }57 else if (arr[cur] == ')') {58 //choice one: keep this ')' in stringbuffer, but in order to avoid duplicate, has to keep all consecutive ')'59 int i=0;60 while (cur+i
0) {69 dfs(arr, res, cur+1, diff, openN, closeN-1, sb);70 }71 }72 }73 74 }

 

转载地址:http://rvlpa.baihongyu.com/

你可能感兴趣的文章
下载器简单实例
查看>>
java实现分页工具类(JDBC)
查看>>
欧几里德算法与扩展欧几里德算法
查看>>
Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)
查看>>
通过kafka提供的命令来查看offset消费情况
查看>>
oracle数据库从入门到精通之四
查看>>
自定义圆形图片控件
查看>>
sharepoint 2013 补丁升级步骤
查看>>
asp.net core 2.0 web api基于JWT自定义策略授权
查看>>
Skype for Business Server 2015-04-前端服务器-3-安装-管理工具
查看>>
第12章代码《跟老男孩学习Linux运维:Shell编程实战》
查看>>
我们为什么从Python转到go?
查看>>
5.Azure负载均衡(上)
查看>>
轻松精通awk数组企业问题案例
查看>>
26.Azure备份服务器(下)
查看>>
从“网上说的能信么”说开去---学习的思考
查看>>
DHCP 日志分析
查看>>
.NET Micro Framework动态调用C/C++底层代码(原理篇)
查看>>
Windows Server 2012正式版RDS系列⒃
查看>>
Shell脚本之awk篇
查看>>