题目如下:

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

代码如下:

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. fedora23帮定键盘系统操作快捷键

    在All settings -> keyboard 主要是以super为主, 然后有 super+ shift+...虽然感觉用 ctrl+super+... 来组合更方便, 但是用 shift ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_5_File类获取功能的方法

    获取的方法 GetAbsolutepath 传递一个相对路径进去,查看输出的结果 输出的还是绝对的路径 getPath 获取的就是构造方法中传递的路径,可以传递绝对路径也可以传递相对路径 实际上toS ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_11_字节输入流一次读取多个字节

    参数带字节数组的 把字节数组转换为字符串 文件里面有ABCDE 再来读取一次 再来读取一次.读取的结果是ED 再来读取,-1到时输出了.但是还是把ED读取出来了. 原理 第一步创建流对象 第二部创建数 ...

  4. delphi开发实例:保存字体设置的方法

    http://blog.csdn.net/delphi308/article/details/9906147 delphi开发实例:保存字体设置的方法 2013-08-11 22:37 446人阅读  ...

  5. EL表达式.jsp指令等

    1.JSP标准指令:<%@ 标准指令(属性 )%><%@ page %><%@ include %><%@ taglib %> 2.JSP程序代码元素: ...

  6. 剑指offer--day11

    1.1 题目:字符串的排列:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba ...

  7. 以区间DP为前提的【洛谷p1063】能量项链

    (跑去练习区间DP,然后从上午拖到下午qwq) 能量项链[题目链接] 然后这道题也是典型的区间DP.因为是项链,所以显然是一个环,然后我们可以仿照石子合并一样,把一个有n个节点的环延长成为有2*n个节 ...

  8. React父子组件间的传值

    父组件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Com ...

  9. 判断当前终端是手机还是pc端并进行不同的页面跳转

    判断当前设备(终端)是手机还是pc端并进行不同的页面跳转 DEMO 1 <script type="text/javascript"> function browser ...

  10. sql server 应用bcp进行数据导出导入

    bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容量复制数据. 使用 bcp 实用工具可以将大量新行导入 SQL Server 表,或将表数据导出到数 ...