【leetcode】1249. Minimum Remove to Make Valid Parentheses
题目如下:
Given a string s of
'(',')'and lowercase English characters.Your task is to remove the minimum number of parentheses (
'('or')', in any positions ) so that the resulting parentheses string is valid and return any valid string.Formally, a parentheses string is valid if and only if:
- It is the empty string, contains only lowercase characters, or
- It can be written as
AB(Aconcatenated withB), whereAandBare valid strings, or- It can be written as
(A), whereAis a valid string.Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.Example 4:
Input: s = "(a(b(c)d)"
Output: "a(b(c)d)"Constraints:
1 <= s.length <= 10^5s[i]is one of'(',')'and lowercase English letters.
解题思路:本题不难,和以前很多括号问题解法类似。从头开始遍历s,如果为左括号,记录其下标,存入数组 left_unpaired中;如果是右括号,找到与其最近的左括号,并将对应左括号从left_uncompair删除,如果没有最近的括号,将右括号存入 right_unpaired中。最后,只要删掉所有存在于 left_unpaired或 right_unpaired中的括号即可。
代码如下:
class Solution(object):
def minRemoveToMakeValid(self, s):
"""
:type s: str
:rtype: str
"""
left_uncompair = []
left_remove = {}
right_remove = {}
for i in range(len(s)):
if s[i] == '(':
left_uncompair.append(i)
left_remove[i] = 1
elif s[i] == ')':
if len(left_uncompair) <= 0:
right_remove[i] = 1
else:
inx = left_uncompair.pop(-1)
del left_remove[inx] res = ''
for i in range(len(s)):
if i in right_remove or i in left_remove:
continue
res += s[i]
return res
【leetcode】1249. Minimum Remove to Make Valid Parentheses的更多相关文章
- LeetCode 1249. Minimum Remove to Make Valid Parentheses
原题链接在这里:https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/ 题目: Given a string s ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】310. Minimum Height Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...
- 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
随机推荐
- LINQ查询表达式详解(2)——查询表达式的转换
简介 C#在执行LINQ查询表达式的时候,并不会指定其执行语义,而是将查询表达式转换为遵循查询表达式模式的方法的调用.具体而言,查询表达式将转换为以下名称的调用:Where.Select.Select ...
- Sublime Text 3 注册激活码
Sublime Text 3 注册激活码 ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 F15 ...
- 手写一个python迭代器
分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器. 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法. 在一个类的实例对象想要变成迭代器,就 ...
- js日期相关方法
/** * ===================================== * 日期相关方法 * ===================================== */ ;(fu ...
- 修改公司VS_UCOS工程BUG调试过程说明
说明:公司里的工程中,使用VS_UCOS来调试应用程序.业务逻辑.方法是嵌入式和VS分别建一个工程,把底层驱动部分分别添加各自需要的源文件,头文件使用同一个.也就是嵌入式的驱动函数名和参数和VS的函数 ...
- python基础之函数进阶
假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print(bi ...
- 北大 ACM highways问题研究(最小生成树)
#include<stdlib.h> #include<stdio.h> #include<queue> struct vertex//代表一个村庄 { int m ...
- 比特(bit)和字节(Byte)
比特(bit)和字节(Byte) 基础的内容就不说了,这里是一个小的学习笔记 比特和字节的写法差异与应用场景 标准的写法中,正如标题中写的那样,是通过大小写来区分比特和字节的:比特的b应该是小写,而字 ...
- 关于redis的几件小事(五)redis保证高并发以及高可用
如果你用redis缓存技术的话,肯定要考虑如何用redis来加多台机器,保证redis是高并发的,还有就是如何让Redis保证自己不是挂掉以后就直接死掉了,redis高可用 redis高并发:主从架构 ...
- 平时工作使用到的idea快捷键或者技巧
平时工作使用到的idea快捷键或者技巧 alt+enter 快速导入包 alt+insert 快速生成setter和getter ctrl+alt+l 格式化代码 /**然后回车 快速生成文档注释 a ...