题目如下:

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

代码如下:

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. day52—JavaScript拖拽事件的应用(自定义滚动条)

    转行学开发,代码100天——2018-05-07 前面的记录里展示了JavaScript中鼠标拖拽功能,今天利用拖拽功能实现另一个应用场景——自定义滚动条(作为控制器)的用法. 常通过自定义滚动条控制 ...

  2. 操作Redis--hash/key-value

    redis也是一个数据库,它的存储以key-value的方式存放,比如: a.关系型数据库 比如: mysql.oracle.sql server.db2.sqlite数据库,为关系型数据库 数据通过 ...

  3. Makefile之patsubst

     经常要手写项目的Makefile,或者看其他项目的遗留项目的Makefile,有些makefile内置函数常用, 却用完就忘记了,最近项目中使用patsubst,感觉挺好用的     格式:$(pa ...

  4. IDEA-关闭自动保存&标志修改文件为星号(一)

    IDEA优化 intellij 关闭自动保存

  5. linux中常用的60个命令及作用详解

    Linux 必学的 60 个命令 Linux 提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在 Linux 系统上工作离不开使用系统 ...

  6. spring-第十六篇之AOP面向切面编程之Spring AOP

    1.上一篇介绍了AspectJ在AOP的简单应用,让我们了解到它的作用就是:开发者无需修改源代码,但又可以为这些组件的方法添加新的功能. AOP的实现可分为两类(根据AOP修改源码的时机划分): 1& ...

  7. 关于Python程序的运行方面,有什么手段能提升性能?

    1.使用多进程,充分利用机器的多核性能 2.对于性能影响较大的部分代码,可以使用C或C++编写 3.对于IO阻塞造成的性能影响,可以使用IO多路复用来解决 4.尽量使用Python的内建函数 5.尽量 ...

  8. Python 内置函数raw_input()和input()用法和区别

    我们知道python接受输入的raw_input()和input() ,在python3 输入raw_input() 去掉乐,只要用input() 输入,input 可以接收一个Python表达式作为 ...

  9. Eclipse打包可执行jar包操作步骤

    1.右键点击工程,选择Export…,进入页面 2.弹出对话框,选择Java->Runnable JAR file ,点击Next>,页面显示jar包的输出路径,配置为jmeter的/li ...

  10. Python笔记(读取txt文件中的数据)

    在机器学习中,常常需要读取txt文本中的数据,这里主要整理了两种读取数据的方式 数据内容 共有四列数据,前三列为特征值,最后一列为数据标签 40920 8.326976 0.953952 3 1448 ...