【leetcode】301. Remove Invalid Parentheses
题目如下:

解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高。括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示这个区间是不合法的,需要删掉一个右括号;遍历完成后,如果左括号数量大于右括号的数量,那么需要删除左括号,直至两者相等。
代码如下:
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的更多相关文章
- 【leetcode】1021. Remove Outermost Parentheses
题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- [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 ...
- 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 ...
随机推荐
- MySQL-default设置
Both statements insert a value into the phone column, but the first inserts a NULL value and the sec ...
- 红米note2 刷机 注意问题:
其他的百度都有,用刷线宝刷 红米note2 刷机 注意问题: 关机状态线下,链接电脑,按着音量下键不松手,按电源键开机后松开,即进入刷机模式. 其中,红米,红米1s移动,红米note移动3g/联通 ...
- 如何解决IIS配置HTTPS证书后刷新消失问题
IIS配置CER证书后完成证书申请后刷新后就会消失的这个BUG微软一直存在,因为我们一般申请都是下来的CER文件和私钥 但是IIS只支持PFX文件的导入,所以我们需要把CER文件和证书私钥转换成PFX ...
- 002/Node.js(Mooc)--Http知识
1.什么是Http 菜鸟教程:http://www.runoob.com/http/http-tutorial.html 视频地址:https://www.imooc.com/video/6713 h ...
- python 入门学习之anaconda篇
还没下载的同学先点击这里进入anaconda官方网站进行下载. 然后点击安装,注意的是这里 安装好了之后呢,我们就开始进行Conda的环境管理,Conda的环境管理功能允许我们同时安装 若干不同版本的 ...
- Django密码错误报错提醒
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOwAAAIBCAYAAABKllNhAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw
- hive Hbase sql
Hive和HBase的区别 hive是为了简化编写MapReduce程序而生的,使用MapReduce做过数据分析的人都知道,很多分析程序除业务逻辑不同外,程序流程基本一样.在这种情况下,就需要h ...
- JDK11 | 第二篇 : JShell 工具
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/saHBSTo4OjsIIqv_ixigjg 一.简介 Java Shell工具是JDK1. ...
- HDU 4549题解 & luogu【模板】矩阵加速(数列)
M斐波那契数列 此题对数学基础要求较高 来源矩阵乘法_百度百科 一个m*n的矩阵是一个由m行n列元素排成的矩形阵列.矩阵里的元素可以是数字符号或者数学式. 形如[acbd][abcd]的数表称为二阶矩 ...
- python 合并字典/拼接字典
针对于python 3.5以上版本: 最好的最快的最优雅的方法是: result_dict = {**dict_1, **dict_2} 例如:( dict 代表 dictionary,也就是字典) ...