[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 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: [""]
这个题目的思路参考地址,是用Java写的, 不得不说, 太牛逼了.
主要思路我理解为, 利用Count去看一个valid parentheses, 然后一旦发现不是valid的时候, 那么我们就在目前的部分string里面, 分别将 ')' 去掉一个, 但是这样会出现的问题是我们可能有重复的情况, 比如说s = ()), we can remove s[1] or s[2] but the result is the same (). Thus, we restrict ourself to remove the first ) in a series of concecutive )s.
那么去掉多余的')' 之后, prefix 变成valid, 然后再recursively call 剩下的string, 但是我们只需要再从上次remove掉的那个位置开始检测即可,否则的话又有重复的. For this, we keep tracking the last removal position and only remove ‘)’ after that.
以上的做法就是去掉多余的')', 比如"()()())", 但是没有办法解决"()()("这样的情况, 那么思路类似,就是将以上的做法, 从右向左走一遍, 然后count计数的时候调换下顺序即可, 但是这个思路牛逼的地方就是,它将s reverse, 然后还是用原来的code, 只是加多一个parameter pairs即可. 实在是太牛逼了. 如果还是看不懂的话建议去看上面参考地址的解释, 很详细, 都是英文, 但是解释的肯定比我好, 比我清楚. 不得不再说一次, 真的太腻害了!
Update from 04/10/2020:
必须有prej,不能只有prei,否则的话input为“()()(())))()”, 因为会错过在有‘)’之前的多余的选择。
有prej:
['(((())))()', '(()(()))()', '()((()))()', '()()(())()']
无prej:
['(()(()))()', '()((()))()', '()()(())()']
1. Constraints
1) s可以为empty
2) 每个元素可以是除了"()"之外的元素
2. Idea
利用所谓的recursive helper function 去帮忙, 不知道时间复杂度是多少.
3.Code:
class Solution:
def removeInvalidParentheses(self, s):
def helper(s, ans, prei, prej, pairs):
count = 0
for i in range(prei, len(s)):
if s[i] == pairs[0]: count += 1
if s[i] == pairs[1]: count -= 1
if count >= 0: continue
for j in range(prej, i+1): # note it is i+1, because until i
if s[j] == pairs[1] and (j == prej or s[j-1] != s[j]):
helper(s[:j] + s[j+1:], ans, i, j, pairs) # should be i + 1, and j + 1, but delete s[j], so i + 1 - 1, j + 1 - 1, in the end i and j
return # the reason it has a return here is finish the helper if it gets into the loop j, otherwise will keep looping for i even the counter is -1.
revs = s[::-1]
if pairs[0] == '(':
helper(revs, ans, 0, 0, ")(")
else:
ans.append(revs)
18 ans = []
19 helper(s, ans, 0, 0, "()")
20 return ans
4. Test cases
1) ""
2) "()"
3) "())"
4) "(()"
5)
"()())()"
[LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS的更多相关文章
- [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
原题链接在这里:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of i ...
- [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
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- 301. Remove Invalid Parentheses
题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- 301. Remove Invalid Parentheses去除不符合匹配规则的括号
[抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...
- [LeetCode] 310. Minimum Height Trees_Medium tag: BFS
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [LeetCode] 101. Symmetric Tree_ Easy tag: BFS
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- PCL—低层次视觉—关键点检测(Harris)
除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更敏感.Harris算法是图像检测识别算法中非常重要的一个算法,其对物体姿态变化鲁棒性好,对旋转不敏感,可以很 ...
- jQuery属性操作(二)
挂载到$上的几个属性操作方法分析,发现属性操作用到了sizzle封装的方法 attr: function( elem, name, value ) { var hooks, ret, ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十四:储存模块
实验十四比起动手笔者更加注重原理,因为实验十四要讨论的东西,不是其它而是低级建模II之一的模块类,即储存模块.接触顺序语言之际,“储存”不禁让人联想到变量或者数组,结果它们好比数据的暂存空间. . i ...
- C# List<T> 泛型
1.简介 所属命名空间:System.Collections.Generic List<T>类是 ArrayList 类的泛型等效类.该类使用大小可按需动态增加的数组实现 IList< ...
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...
- HTML display:inline-block
元素转换 display:block; 行内转块 Display:inline; 块转行内 Display:inline-block; 块或行内转行内块 链接伪类 a:link{属性:值;} ...
- git如何回滚当前修改的内容?
git如何回滚当前修改的内容? 1.打开git gui,在工具栏上点击 commit ,选择 Revert Changes, 这里可以回滚单个文件: 2.一键回滚所有修改: 打开git gui,在工 ...
- 8.20 前端 js
2018-8-20 17:40:12 js参考: https://www.cnblogs.com/liwenzhou/p/8004649.html 2018-8-20 20:33:31 css学完了 ...
- iOS Swift 实现图片点击缩放回弹动画
效果就是下面这个样子: 思路借鉴的是MZTimerLabel,有想过做一个自定义的ImageView,但那样的话之前view用必须要改代码,索性就按照MZTimerLabel这个方式实现,简单易用,从 ...
- POJ-2346 Lucky tickets(线性DP)
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...