【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 ...
随机推荐
- kafka 配置权限
参考:https://www.cnblogs.com/huxi2b/p/10437844.html http://kafka.apache.org/documentation/#security_au ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第8节 Math类_19_Math练习:小学数学真题
题目 画数轴 解题思路 强转成int类型就会舍弃小数位数 输出最终的数量 如果用Math.ceil的方式的话
- 多次最短路反思-Holy Grail
√ u=s[run],v=t[run]; ret=max(-d[v][u],-1000000000LL); dis[u][v]=ret;//d[u][v]= G[u].push_back(v); × ...
- Prometheus + AlertManager 邮件报警
安装 wget https://github.com/prometheus/alertmanager/releases/download/v0.13.0/alertmanager-0.13.0.lin ...
- React.memo
介绍React.memo之前,先了解一下React.Component和React.PureComponent. React.Component React.Component是基于ES6 class ...
- vue 跳转页面返回时tab状态有误的解决办法
一.前言 最近在做新vue项目的时候遇到了一个问题,就是tab间的切换没有问题,当跳转到其他页面时,且这个页面并非子路由,再用浏览器的返回按钮返回首页时,tab的active始终指向默认的第一个选项. ...
- bzoj3188 [Coci 2011]Upit(分块)
Time Limit: 10 Sec Memory Limit: 128 MB Description 你需要维护一个序列,支持以下4种操作.一,将区间(u,v)的数覆盖为C:二,将区间(u,v)的 ...
- Cocos2d-x的Android配置以及相关參考文档
版权声明:版权声明:本文为博主原创文章.转载请附上博文链接! https://blog.csdn.net/ccf19881030/article/details/24141181 关于Win7 ...
- CSS-子盒子撑开父盒子,让父盒子的高随内容自适应
方法一: height:auto!important; height:200px; min-height:200px; ie6并不支持min-height.ie7,opera,火狐没有问题. 方法二: ...
- Hadoop MapReduce实现人员二度关系运算
1.一度人脉:双方直接是好友 2.二度人脉:双方有一个以上共同的好友,这时朋友网可以计算出你们有几个共同的好友并且呈现数字给你.你们的关系是: 你->朋友->陌生人 3.三度人脉:即你朋友 ...