力扣:1021:删除最外层的括号

This commit is contained in:
huangge1199 2021-04-01 12:47:09 +08:00
parent 9d1ee7ea99
commit 8b01df8da0
2 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,79 @@
//有效括号字符串为空 ("")"(" + A + ")" A + B其中 A B 都是有效的括号字符串+ 代表字符串的连接例如"""()"
//"(())()" "(()(()))" 都是有效的括号字符串
//
// 如果有效字符串 S 非空且不存在将其拆分为 S = A+B 的方法我们称其为原语primitive其中 A B 都是非空有效括号字符串
//
// 给出一个非空有效字符串 S考虑将其进行原语化分解使得S = P_1 + P_2 + ... + P_k其中 P_i 是有效括号字符串原语
//
// S 进行原语化分解删除分解中每个原语字符串的最外层括号返回 S
//
//
//
// 示例 1
//
// 输入"(()())(())"
//输出"()()()"
//解释
//输入字符串为 "(()())(())"原语化分解得到 "(()())" + "(())"
//删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"
//
// 示例 2
//
// 输入"(()())(())(()(()))"
//输出"()()()()(())"
//解释
//输入字符串为 "(()())(())(()(()))"原语化分解得到 "(()())" + "(())" + "(()(()))"
//删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"
//
//
// 示例 3
//
// 输入"()()"
//输出""
//解释
//输入字符串为 "()()"原语化分解得到 "()" + "()"
//删除每个部分中的最外层括号后得到 "" + "" = ""
//
//
//
//
// 提示
//
//
// S.length <= 10000
// S[i] "(" ")"
// S 是一个有效括号字符串
//
// Related Topics
// 👍 163 👎 0
package leetcode.editor.cn;
//1021:删除最外层的括号
public class RemoveOutermostParentheses {
public static void main(String[] args) {
//测试代码
Solution solution = new RemoveOutermostParentheses().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String removeOuterParentheses(String S) {
int count = 0;
String result = "";
for (char ch : S.toCharArray()) {
if (ch == '(') {
count++;
result = count > 1 ? result + ch : result;
}else{
count--;
result = count > 0 ? result + ch : result;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,46 @@
<p>有效括号字符串为空&nbsp;<code>(&quot;&quot;)</code><code>&quot;(&quot; + A + &quot;)&quot;</code>&nbsp;&nbsp;<code>A + B</code>,其中&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;都是有效的括号字符串,<code>+</code>&nbsp;代表字符串的连接。例如,<code>&quot;&quot;</code><code>&quot;()&quot;</code><code>&quot;(())()&quot;</code>&nbsp;&nbsp;<code>&quot;(()(()))&quot;</code>&nbsp;都是有效的括号字符串。</p>
<p>如果有效字符串&nbsp;<code>S</code>&nbsp;非空,且不存在将其拆分为&nbsp;<code>S = A+B</code>&nbsp;的方法,我们称其为<strong>原语primitive</strong>,其中&nbsp;<code>A</code>&nbsp;<code>B</code>&nbsp;都是非空有效括号字符串。</p>
<p>给出一个非空有效字符串&nbsp;<code>S</code>,考虑将其进行原语化分解,使得:<code>S = P_1 + P_2 + ... + P_k</code>,其中&nbsp;<code>P_i</code>&nbsp;是有效括号字符串原语。</p>
<p>&nbsp;<code>S</code>&nbsp;进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 <code>S</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>&quot;(()())(())&quot;
<strong>输出:</strong>&quot;()()()&quot;
<strong>解释:
</strong>输入字符串为 &quot;(()())(())&quot;,原语化分解得到 &quot;(()())&quot; + &quot;(())&quot;
删除每个部分中的最外层括号后得到 &quot;()()&quot; + &quot;()&quot; = &quot;()()()&quot;</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>&quot;(()())(())(()(()))&quot;
<strong>输出:</strong>&quot;()()()()(())&quot;
<strong>解释:</strong>
输入字符串为 &quot;(()())(())(()(()))&quot;,原语化分解得到 &quot;(()())&quot; + &quot;(())&quot; + &quot;(()(()))&quot;
删除每个部分中的最外层括号后得到 &quot;()()&quot; + &quot;()&quot; + &quot;()(())&quot; = &quot;()()()()(())&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>&quot;()()&quot;
<strong>输出:</strong>&quot;&quot;
<strong>解释:</strong>
输入字符串为 &quot;()()&quot;,原语化分解得到 &quot;()&quot; + &quot;()&quot;
删除每个部分中的最外层括号后得到 &quot;&quot; + &quot;&quot; = &quot;&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li><code>S.length &lt;= 10000</code></li>
<li><code>S[i]</code>&nbsp;<code>&quot;(&quot;</code>&nbsp;<code>&quot;)&quot;</code></li>
<li><code>S</code> 是一个有效括号字符串</li>
</ol>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 163</li><li>👎 0</li></div>