当前位置:网站首页>模拟卷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,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Buuctf-[[gwctf 2019] I have a database (xiaoyute detailed explanation)
- Manhattan distance sum - print diamond
- What are the test sites for tunnel engineering?
- 通过修改style设置打印页样式
- LeetCode 732. 我的日程安排表 III
- Application du Groupe Li dans gtsam
- Seven imperceptible truths in software testing
- ESP32 ESP-IDF看门狗TWDT
- [postman] the monitors monitoring API can run periodically
- Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
猜你喜欢
随机推荐
自定义指定路由上的Gateway过滤器工厂
2022 software testing workflow to know
Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
[wechat applet] build a development tool environment
单元测试的意义
【C语言】qsort函数
【Postman】Monitors 监测API可定时周期运行
selenium源码通读·9 |DesiredCapabilities类分析
【微信小程序】搭建开发工具环境
[postman] collections configuration running process
MFC 动态创建的对话框及改变控件的大小和位置
职场进阶指南:大厂人必看书籍推荐
Embedded point test of app
在线问题与离线问题
G - Supermarket
D - How Many Answers Are Wrong
JWT-JSON WEB TOKEN
这些年用Keil遇到的坑
How to extract login cookies when JMeter performs interface testing
對數據安全的思考(轉載)

![[postman] collections - run the imported data file of the configuration](/img/85/7ac9976fb09c465c88f376b2446517.png)







