1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-21 04:56:46 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p>给你一个括号字符串&nbsp;<code>s</code>&nbsp;,它只包含字符&nbsp;<code>&#39;(&#39;</code> 和&nbsp;<code>&#39;)&#39;</code>&nbsp;。一个括号字符串被称为平衡的当它满足:</p>\n\n<ul>\n\t<li>任何左括号&nbsp;<code>&#39;(&#39;</code>&nbsp;必须对应两个连续的右括号&nbsp;<code>&#39;))&#39;</code>&nbsp;。</li>\n\t<li>左括号&nbsp;<code>&#39;(&#39;</code>&nbsp;必须在对应的连续两个右括号&nbsp;<code>&#39;))&#39;</code>&nbsp;之前。</li>\n</ul>\n\n<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>\n\n<p>你可以在任意位置插入字符 &#39;(&#39; 和 &#39;)&#39; 使字符串平衡。</p>\n\n<p>请你返回让 <code>s</code>&nbsp;平衡的最少插入次数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre><strong>输入:</strong>s = &quot;(()))&quot;\n<strong>输出:</strong>1\n<strong>解释:</strong>第二个左括号有与之匹配的两个右括号,但是第一个左括号只有一个右括号。我们需要在字符串结尾额外增加一个 &#39;)&#39; 使字符串变成平衡字符串 &quot;(())))&quot; 。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre><strong>输入:</strong>s = &quot;())&quot;\n<strong>输出:</strong>0\n<strong>解释:</strong>字符串已经平衡了。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre><strong>输入:</strong>s = &quot;))())(&quot;\n<strong>输出:</strong>3\n<strong>解释:</strong>添加 &#39;(&#39; 去匹配最开头的 &#39;))&#39; ,然后添加 &#39;))&#39; 去匹配最后一个 &#39;(&#39; 。\n</pre>\n\n<p><strong>示例 4</strong></p>\n\n<pre><strong>输入:</strong>s = &quot;((((((&quot;\n<strong>输出:</strong>12\n<strong>解释:</strong>添加 12 个 &#39;)&#39; 得到平衡字符串。\n</pre>\n\n<p><strong>示例 5</strong></p>\n\n<pre><strong>输入:</strong>s = &quot;)))))))&quot;\n<strong>输出:</strong>5\n<strong>解释:</strong>在字符串开头添加 4 个 &#39;(&#39; 并在结尾添加 1 个 &#39;)&#39; ,字符串变成平衡字符串 &quot;(((())))))))&quot; 。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 10^5</code></li>\n\t<li><code>s</code>&nbsp;只包含&nbsp;<code>&#39;(&#39;</code> 和&nbsp;<code>&#39;)&#39;</code>&nbsp;。</li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 39,
"likes": 41,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -149,7 +149,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"9.8K\", \"totalSubmission\": \"20.3K\", \"totalAcceptedRaw\": 9751, \"totalSubmissionRaw\": 20320, \"acRate\": \"48.0%\"}",
"stats": "{\"totalAccepted\": \"10.6K\", \"totalSubmission\": \"22K\", \"totalAcceptedRaw\": 10589, \"totalSubmissionRaw\": 21992, \"acRate\": \"48.1%\"}",
"hints": [
"Use a stack to keep opening brackets. If you face single closing ')' add 1 to the answer and consider it as '))'.",
"If you have '))' with empty stack, add 1 to the answer, If after finishing you have x opening remaining in the stack, add 2x to the answer."