当前位置:网站首页>模拟卷Leetcode【普通】1249. 移除无效的括号
模拟卷Leetcode【普通】1249. 移除无效的括号
2022-07-06 06:15:00 【邂逅模拟卷】
1249. 移除无效的括号
给你一个由 ‘(’、‘)’ 和小写字母组成的字符串 s。
你需要从字符串中删除最少数目的 ‘(’ 或者 ‘)’ (可以删除任意位置的括号),使得剩下的「括号字符串」有效。
请返回任意一个合法字符串。
有效「括号字符串」应当符合以下 任意一条 要求:
空字符串或只包含小写字母的字符串
可以被写作 AB(A 连接 B)的字符串,其中 A 和 B 都是有效「括号字符串」
可以被写作 (A) 的字符串,其中 A 是一个有效的「括号字符串」
示例 1:
输入:s = “lee(to)de)”
输出:“lee(to)de”
解释:“lee(t(co)de)” , “lee(tode)” 也是一个可行答案。
示例 2:
输入:s = “a)bd”
输出:“abd”
示例 3:
输入:s = “))((”
输出:“”
解释:空字符串也是有效的
示例 4:
输入:s = “(a(bd)”
输出:“a(bd)”
提示:
1 <= s.length <= 10^5
s[i] 可能是 ‘(’、‘)’ 或英文小写字母
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-remove-to-make-valid-parentheses
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def minRemoveToMakeValid(self, s: str) -> str:
left,right = 0,s.count(')')
res = ''
for char in s:
print(char,left,right)
if char=='(':
if right>0:
res += char
left+=1
right-=1
elif char==')':
if left>0:
res += char
left-=1
else:
right-=1
else:
res +=char
return res
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.minRemoveToMakeValid(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
["lee(t(c)o)de)"],
["))(("],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- 【Postman】Monitors 监测API可定时周期运行
- F - true liars (category and search set +dp)
- CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
- Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
- 对数据安全的思考(转载)
- 【C语言】qsort函数
- The latest 2022 review of "graph classification research"
- Win10 cannot operate (delete, cut) files
- 「 WEB测试工程师 」岗位一面总结
- 使用Nacos管理配置
猜你喜欢
![Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)](/img/2c/43ce298794589c5282edda94161d62.jpg)
Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)

What are the test sites for tunnel engineering?

【微信小程序】搭建开发工具环境

技术分享 | 常见接口协议解析

F - true liars (category and search set +dp)

Overview of three core areas of Mathematics: algebra

(中)苹果有开源,但又怎样呢?

LeetCode 732. 我的日程安排表 III

On weak network test of special test

【eolink】PC客户端安装
随机推荐
LeetCode 731. 我的日程安排表 II
Overview of three core areas of Mathematics: geometry
B - The Suspects
二维码的前世今生 与 六大测试点梳理
D - How Many Answers Are Wrong
MFC关于长字符串unsigned char与CString转换及显示问题
Understanding of processes and threads
假设检验学习笔记
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
IP day 16 VLAN MPLS configuration
【Postman】测试(Tests)脚本编写和断言详解
Aike AI frontier promotion (2.13)
曼哈顿距离和-打印菱形
Resttemplate and feign realize token transmission
Eigen稀疏矩阵操作
[postman] collections - run the imported data file of the configuration
职场进阶指南:大厂人必看书籍推荐
Isam2 and incrementalfixedlagsmooth instructions in gtsam
How to extract login cookies when JMeter performs interface testing
Career advancement Guide: recommended books for people in big factories