Given a parentheses string s containing only the characters ‘(’ and ‘)’. A parentheses string is balanced if:
Any left parenthesis ‘(’ must have a corresponding two consecutive right parenthesis ‘))’.
Left parenthesis ‘(’ must go before the corresponding two consecutive right parenthesis ‘))’.
In other words, we treat ‘(’ as an opening parenthesis and ‘))’ as a closing parenthesis.
For example, “())”, “())(())))” and “(())())))” are balanced, “)()”, “()))” and “(()))” are not balanced.
You can insert the characters ‘(’ and ‘)’ at any position of the string to balance it if needed.
Return the minimum number of insertions needed to make s balanced.
Example 1:
Input: s = “(()))”
Output: 1
Explanation: The second ‘(’ has two matching ‘))’, but the first ‘(’ has only ‘)’ matching. We need to add one more ‘)’ at the end of the string to be “(())))” which is balanced.
Example 2:
Input: s = “())”
Output: 0
Explanation: The string is already balanced.
Example 3:
Input: s = “))())(”
Output: 3
Explanation: Add ‘(’ to match the first ‘))’, Add ‘))’ to match the last ‘(’.
Constraints:
1 <= s.length <= 105
s consists of ‘(’ and ‘)’ only.
解法1:这题其实不简单。关键是来了左括号时,当前所需右括号数目是奇数的处理。
注意:
当来了’(‘括号时,
a. 如果当前所需右括号数目rightNeeded是偶数,那没什么大关系,再把rightNeeded +2就可以。比如输入"(())",rightNeeded=2,那再来一个’(',rightNeeded=4。
b. 如果当前所需右括号数目rightNeeded是奇数,那么比较麻烦,我们必须在新来左括号’(‘的时候消掉一个多余的右括号,这样,rightNeeded就是偶数,跟a的情况一样。怎么消掉呢?那我们再增加左括号,消掉一个右括号就可以了。
比如输入"(()",rightNeeded=3。再来一个’(‘,此时我们必须在这个新来的’(‘前面加上一个’(',即"((()(",这样,leftNeeded++, rightNeeded–,就可以把rightNeeded变成偶数了。
举例:
“(()))(()))()())))”
b. from labuladong 补充一下“当遇到左括号时,若对右括号的需求量为奇数,需要插入 1 个右括号”,这个地方如果不做判断,那么 "()()))“ 和 ")()))“这两种情况(能造成 need为奇数的也就这两种情况,一个是need==-1造成的,一个是本来就缺一个右括号)结果都为0,也就是说不加奇偶判断的话,是不能保证按”())“顺序闭合的,一个左括号出现之前,必须要保证之前的左括号都已经匹配完或者一个右括号也没匹配,所以这句话应该"当遇到左括号时,若对右括号的需求量为奇数,需要插入 1 个右括号,来匹配之前缺一个右括号的左括号”
class Solution {
public:
int minInsertions(string s) {
//int n = s.size();
int leftMustAdd = 0, rightMustAdd = 0, leftNeed = 0, rightNeed = 0;
for (auto c : s) {
if (c == '(') {
rightNeed += 2;
if (rightNeed & 0x1) { //consider "()()))" and ")()))"
//leftMustAdd++;
rightMustAdd++;
rightNeed--;
}
} else { // ')'
rightNeed--;
if (rightNeed == -1) {
leftMustAdd++;
rightNeed = 1;
}
}
}
return leftMustAdd + rightMustAdd + leftNeed + rightNeed;
}
};
谈谈我的理解: