当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
猜你喜欢
Hypothesis testing learning notes
全链路压测:构建三大模型
【Postman】Monitors 监测API可定时周期运行
把el-tree选中的数组转换为数组对象
【Postman】Collections配置运行过程
曼哈顿距离和-打印菱形
误差的基本知识
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
[postman] collections configuration running process
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
随机推荐
JWT-JSON WEB TOKEN
多线程应用的测试与调试
数字三角形模型 AcWing 1015. 摘花生
Embedded point test of app
【微信小程序】搭建开发工具环境
模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
Reading notes of effective managers
php使用redis实现分布式锁
Idea new UI usage
Left matching principle of joint index
全链路压测:构建三大模型
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
[ram IP] introduction and experiment of ram IP core
职场进阶指南:大厂人必看书籍推荐
浅谈专项测试之弱网络测试
F - True Liars (种类并查集+DP)
MFC 动态创建的对话框及改变控件的大小和位置
模拟卷Leetcode【普通】1219. 黄金矿工
把el-tree选中的数组转换为数组对象
[postman] test script writing and assertion details