【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
(A
concatenated withB
), whereA
andB
are valid strings, or- It can be written as
(A)
, whereA
is 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^5
s[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. ...
随机推荐
- 【DSP开发】TMS320C66x DSP SPI Nor Flash的程序烧写
经过一段时间的研究终于把TMS320C6657单核和双核的SPI Nor Flash的程序烧写调通了.工具都是前辈的工作,有需要的可以留下邮箱,我有空可以发. 原理参考钱丰的<TI c66x 系 ...
- 第七次java实验报告
Java实验报告 班级 计科二班 学号20188437 姓名 何磊 完成时间 2019/10/25 评分等级 实验四 类的继承 实验内容 )总票数1000张:(2)10个窗口同时开始卖票:(3)卖票过 ...
- 【Spring 源码】Spring 加载资源并装配对象的过程(XmlBeanDefinitionReader)
Spring 加载资源并装配对象过程 在Spring中对XML配置文件的解析从3.1版本开始不再推荐使用XmlBeanFactory而是使用XmlBeanDefinitionReader. Class ...
- 开启httpfs
参考: https://docs.cloudera.com/documentation/enterprise/6/6.2/topics/admin_httpfs.html#xd_583c10bfdbd ...
- vue : 无法加载文件 C:\Users\lihongjie\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 htt ps:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1 + vue init webpack vue_p
以管理员方式打开powershell 运行命令:set-ExecutionPolicy RemoteSigned 出现: 执行策略更改执行策略可帮助你防止执行不信任的脚本.更改执行策略可能会产生安全风 ...
- 关于VUE 配置文件config详解内容
// const path = require('path'); module.exports = { /** 区分打包环境与开发环境 * process.env.NODE_ENV==='produc ...
- 使用正则实现php的trim函数,支持全角空格
之前使用trim来移除一段文字开头的空格,移除不掉,发现是全角空格的锅. 便专门添加对全角空格的移除: trim($str," "); 但是效果并不好,因为trim函数对多字节字符 ...
- python网络爬虫(12)去哪网酒店信息爬取
目的意义 爬取某地的酒店价格信息,示例使用selenium在Firefox中的使用. 来源 少部分来源于书.python爬虫开发与项目实战 构造 本次使用简易的方案,模拟浏览器访问,然后输入字段,查找 ...
- 二、redis学习(java操作redis缓存的工具jedis)
- pytorch中torch.narrow()函数
torch.narrow(input, dim, start, length) → Tensor Returns a new tensor that is a narrowed version of ...