题目如下:

解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高。括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号;遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等。

代码如下:

class Solution(object):
def removeInvalidParentheses(self, s):
"""
:type s: str
:rtype: List[str]
"""
queue = [(s,0)]
res = []
dic = {}
while len(queue) > 0:
qs,count = queue.pop(0)
left = right = 0
flag = False
for i,v in enumerate(qs):
if v == '(':
left += 1
elif v == ')':
right += 1
if right > left:
for j in range(i+1):
if qs[j] == ')':
newqs = qs[:j] + qs[j+1:]
if (newqs, count + 1) not in queue:
queue.append((newqs,count+1))
flag = True
break
if flag == True:
continue
if left == right:
if qs not in dic:
dic[qs] = 1
if len(res) == 0:
res.append((qs,count))
else:
if res[-1][1] > count:
res = [(qs,count)]
elif res[-1][1] == count:
res.append((qs,count))
else:
continue
else:
for j, v in enumerate(qs):
if v == '(':
newqs = qs[:j] + qs[j+1:]
if (newqs,count+1) not in queue:
queue.append((newqs,count+1))
ans = []
for i in res:
ans.append(i[0])
return ans

【leetcode】301. Remove Invalid Parentheses的更多相关文章

  1. 【leetcode】1021. Remove Outermost Parentheses

    题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...

  2. 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  3. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  4. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

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

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

  6. LeetCode 301. Remove Invalid Parentheses

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

  7. [leetcode]301. Remove Invalid Parentheses 去除无效括号

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

  8. 301. Remove Invalid Parentheses

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

  9. 301. Remove Invalid Parentheses去除不符合匹配规则的括号

    [抄题]: Remove the minimum number of invalid parentheses in order to make the input string valid. Retu ...

随机推荐

  1. commons-collections包中的常用的工具类

    commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...

  2. codeforces 657C - Bear and Contribution [想法题]

    题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...

  3. Rhybox播放mp3, smplayer如何播放flv等等

    [[ 支持mp3,在终端: sudo apt-get install gstreamer0.10-*plugins-ugly 支持wma,在终端: sudo apt-get install gstre ...

  4. 使用Chrome逆向分析JS实战---分析google网站翻译器原文存放位置

    剧透:就是使用了一下Chrome DevTools的Memory功能,通过已知的JS变量的值查找JS内存中变量的引用 一:不分析一下现有的网页翻译方法么? 总所周知,(As is well known ...

  5. iptables中文帮助

    Iptables(8)                                                                                          ...

  6. vue组件父子间通信02

    三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"> ...

  7. 应用安全 - 安全设备 - WAF原理/检测/绕过

    原理 基于Cookie值 Citrix Netscaler(2013年使用广泛) “Citrix Netscaler”会在HTTP返回头部Cookie位置加入“ns_af”的值,可以以此判断为Citr ...

  8. vue 中注册全局组件

    1  全局注册组件 建一个 js 文件, 注册全局组件, 并且暴露出去 然后再在 main.js  中引入       在页面就可以直接使用了    2 全局注册过滤器 建立文件, 包含所有过滤器方法 ...

  9. AtCoder Beginner Contest 133 -D — Rain Flows into Dams

    (https://atcoder.jp/contests/abc133/tasks/abc133_d) 思路:每座山为2Xi,每个坝为Ai.已知Ai,求出2Xi. 根据已知的X1,则可分别求出X2-n ...

  10. 回溯---N皇后

    N 皇后 51. N-Queens (Hard) 题目描述:   在n*n的矩阵中摆放n个皇后,并且每个皇后不能在同一列,同一个对角线上,求所有的n皇后解. 思路分析:   一行一行地摆放,在确定一行 ...