[LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string `S` of `'('` and `')'` parentheses, we add the minimum number of parentheses ( `'('` or `')'`, and in any positions ) so that the resulting parentheses string is valid.
Formally, a parentheses string is valid if and only if:
- It is the empty string, or
- It can be written as
AB(Aconcatenated withB), whereAandBare valid strings, or - It can be written as
(A), whereAis a valid string.
Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.
Example 1:
Input: "())"
Output: 1
Example 2:
Input: "((("
Output: 3
Example 3:
Input: "()"
Output: 0
Example 4:
Input: "()))(("
Output: 4
Note:
S.length <= 1000Sonly consists of'('and')'characters.
这道题给了一个括号字符串,可能是非法的,让我们补充最少数量的半括号,使其变为合法的括号字符串。那么实际上只要统计出需要添加的左右括号个数即可,这里使用两个变量 left 和 right,分别表示需要的左右括号个数。遍历字符串S,若遇到左括号,说明此时需要右括号,则 right 自增1;若遇到了右括号,若此时 right 大于0,说明当前的右括号可以用来匹配之前的左括号,不需要另加右括号,所以此时 right 自减1;而若此时 right 为0,说明当前的右括号前面没有左括号可以跟其匹配,则此时 left 自增1,表示需要额外的左括号。最后返回 left+right 即为所求,参见代码如下:
解法一:
class Solution {
public:
int minAddToMakeValid(string S) {
int left = 0, right = 0;
for (char c : S) {
if (c == '(') {
++right;
} else if (right > 0) {
--right;
} else {
++left;
}
}
return left + right;
}
};
我们可以只用一个变量 cnt,表示当前左括号的个数。遍历字符串S,当遇到左括号,而此时 cnt 为负数时,表示此时右括号是多余左括号的,而当前遇到的左括号不能匹配之前的右括号,所以将 cnt 的绝对值加到结果 res 中,表示需要这多么的左括号来匹配之前多出的右括号。然后此时 cnt 自增1,因为当前遇到的是左括号,若当前遇到右括号,则 cnt 自减1,最终返回 res 加上 cnt 的绝对值即为所求,参见代码如下:
解法二:
class Solution {
public:
int minAddToMakeValid(string S) {
int res = 0, cnt = 0;
for (char c : S) {
if (c == '(') {
if (cnt < 0) {
res += abs(cnt);
cnt = 0;
}
++cnt;
} else {
--cnt;
}
}
return res + abs(cnt);
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/921
类似题目:
Different Ways to Add Parentheses
参考资料:
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加的更多相关文章
- 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【leetcode】921. Minimum Add to Make Parentheses Valid
题目如下: 解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧.括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈.遍历Input,如果是'('直接入栈 ...
- 921. Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- leetcode 921. 使括号有效的最少添加(Python)
class Solution: def minAddToMakeValid(self, S): """ :type S: str :rtype: int "&q ...
- leetcode 921. 使括号有效的最少添加
问题描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字符 ...
- [LeedCode]921. 使括号有效的最少添加
题目描述: 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字 ...
- [leetcode-921-Minimum Add to Make Parentheses Valid]
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
随机推荐
- 《一起学mysql》2
事务 爸妈让往他们银行卡里转点儿钱,可是我这钱全在支付宝里,爸妈又没有支付宝,只能从支付宝里转给他 们了,假如转账过程中,支付宝扣款成功了,但是银行系统崩溃了,钱没转进去,这咋整?我的大洋就这样 ...
- kali渗透综合靶机(十四)--g0rmint靶机
kali渗透综合靶机(十四)--g0rmint靶机 靶机下载地址:https://www.vulnhub.com/entry/g0rmint-1,214/ 一.主机发现 1.netdiscover - ...
- Zabbix server 更改数据库密码
Zabbix server 更改数据库密码 当我们的 Zabbix server 使用的数据库信息发生了改变,我们需要更改旧配置. 涉及到数据库配置信息的文件有2个,分别是 /etc/zabbix/z ...
- Winform中使用控件的Dock属性设计窗体布局,使不随窗体缩放而改变
场景 在新建一个Winform窗体后,拖拽控件设置其布局如下 如果只是单纯的这么设计,我们在运行后,如果对窗口进行缩放就会导致如下 所以我们需要在设计页面布局时对控件进行定位设置. 注: 博客主页:h ...
- 你不知道的Golang map
在开发过程中,map是必不可少的数据结构,在Golang中,使用map或多或少会遇到与其他语言不一样的体验,比如访问不存在的元素会返回其类型的空值.map的大小究竟是多少,为什么会报"can ...
- Python学习之路 【目录】
* Python之路[楔子]:PyCharm 专业版安装 * Python之路[第一篇]:Python简介和入门 * Python之路[第二篇]:Python基础(一 ...
- javascript:警告(alert 消息对话框),确认(confirm 消息对话框)
原文:https://blog.csdn.net/u012110719/article/details/41926315
- mssql SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
解决方法: 工具-〉选项-〉左侧有个 设计器-〉表设计器和数据库设计器 -> 阻止保存要求重新创建表的更改(右侧) 把钩去掉即可.
- Git Error:There is no tracking information for the current branch.
在执行git pull的时候,提示当前branch没有跟踪信息: $> git pull There is no tracking information for the current bra ...
- QT 使用QSetting读取配置文件中的中文乱码解决方案
windows下方案: 首先需要将ini文件改成UTF-8或GB2312编码格式,可以通过notepad++工具实现.然后在配置项中填入中文,如下: 接着在程序中使用 QSettings settin ...