1541:平衡括号字符串的最少插入次数

This commit is contained in:
huangge1199 2021-05-31 15:11:22 +08:00
parent 8c0f60e01d
commit a0ba3cdecf
3 changed files with 162 additions and 1 deletions

View File

@ -0,0 +1,102 @@
//给你一个括号字符串 s 它只包含字符 '(' ')' 一个括号字符串被称为平衡的当它满足
//
//
// 任何左括号 '(' 必须对应两个连续的右括号 '))'
// 左括号 '(' 必须在对应的连续两个右括号 '))' 之前
//
//
// 比方说 "())" "())(())))" "(())())))" 都是平衡的 ")()" "()))" "(()))" 都是不平衡的
//
// 你可以在任意位置插入字符 '(' ')' 使字符串平衡
//
// 请你返回让 s 平衡的最少插入次数
//
//
//
// 示例 1
//
// 输入s = "(()))"
//输出1
//解释第二个左括号有与之匹配的两个右括号但是第一个左括号只有一个右括号我们需要在字符串结尾额外增加一个 ')' 使字符串变成平衡字符串 "(())))"
//
//
//
// 示例 2
//
// 输入s = "())"
//输出0
//解释字符串已经平衡了
//
//
// 示例 3
//
// 输入s = "))())("
//输出3
//解释添加 '(' 去匹配最开头的 '))' 然后添加 '))' 去匹配最后一个 '('
//
//
// 示例 4
//
// 输入s = "(((((("
//输出12
//解释添加 12 ')' 得到平衡字符串
//
//
// 示例 5
//
// 输入s = ")))))))"
//输出5
//解释在字符串开头添加 4 '(' 并在结尾添加 1 ')' 字符串变成平衡字符串 "(((())))))))"
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 10^5
// s 只包含 '(' ')'
//
// Related Topics 字符串
// 👍 24 👎 0
package leetcode.editor.cn;
//1541:平衡括号字符串的最少插入次数
public class MinimumInsertionsToBalanceAParenthesesString {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumInsertionsToBalanceAParenthesesString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minInsertions(String s) {
int num = 0;
int result = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
num++;
} else if (s.charAt(i) == ')') {
if (i + 1 < s.length() && s.charAt(i + 1) == ')') {
i++;
} else {
result++;
}
if (num > 0) {
num--;
} else {
result++;
}
}
}
if (num > 0) {
result += num * 2;
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,59 @@
<p>给你一个括号字符串&nbsp;<code>s</code>&nbsp;,它只包含字符&nbsp;<code>&#39;(&#39;</code>&nbsp;<code>&#39;)&#39;</code>&nbsp;。一个括号字符串被称为平衡的当它满足:</p>
<ul>
<li>任何左括号&nbsp;<code>&#39;(&#39;</code>&nbsp;必须对应两个连续的右括号&nbsp;<code>&#39;))&#39;</code>&nbsp;</li>
<li>左括号&nbsp;<code>&#39;(&#39;</code>&nbsp;必须在对应的连续两个右括号&nbsp;<code>&#39;))&#39;</code>&nbsp;之前。</li>
</ul>
<p>比方说&nbsp;<code>&quot;())&quot;</code>&nbsp;<code>&quot;())(())))&quot;</code>&nbsp;<code>&quot;(())())))&quot;</code>&nbsp;都是平衡的,&nbsp;<code>&quot;)()&quot;</code>&nbsp;<code>&quot;()))&quot;</code>&nbsp;<code>&quot;(()))&quot;</code>&nbsp;都是不平衡的。</p>
<p>你可以在任意位置插入字符 &#39;(&#39;&#39;)&#39; 使字符串平衡。</p>
<p>请你返回让 <code>s</code>&nbsp;平衡的最少插入次数。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s = &quot;(()))&quot;
<strong>输出:</strong>1
<strong>解释:</strong>第二个左括号有与之匹配的两个右括号,但是第一个左括号只有一个右括号。我们需要在字符串结尾额外增加一个 &#39;)&#39; 使字符串变成平衡字符串 &quot;(())))&quot;
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = &quot;())&quot;
<strong>输出:</strong>0
<strong>解释:</strong>字符串已经平衡了。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = &quot;))())(&quot;
<strong>输出:</strong>3
<strong>解释:</strong>添加 &#39;(&#39; 去匹配最开头的 &#39;))&#39; ,然后添加 &#39;))&#39; 去匹配最后一个 &#39;(&#39;
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>s = &quot;((((((&quot;
<strong>输出:</strong>12
<strong>解释:</strong>添加 12 个 &#39;)&#39; 得到平衡字符串。
</pre>
<p><strong>示例 5</strong></p>
<pre><strong>输入:</strong>s = &quot;)))))))&quot;
<strong>输出:</strong>5
<strong>解释:</strong>在字符串开头添加 4 个 &#39;(&#39; 并在结尾添加 1 个 &#39;)&#39; ,字符串变成平衡字符串 &quot;(((())))))))&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
<li><code>s</code>&nbsp;只包含&nbsp;<code>&#39;(&#39;</code>&nbsp;<code>&#39;)&#39;</code>&nbsp;</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>字符串</li></div></div>\n<div><li>👍 24</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long