1190:反转每对括号间的子串

This commit is contained in:
huangge1199@hotmail.com 2021-05-12 23:57:14 +08:00
parent 6f0e01e7d8
commit b5dd7542d4
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,102 @@
//给出一个字符串 s仅含有小写英文字母和括号
//
// 请你按照从括号内到外的顺序逐层反转每对匹配括号中的字符串并返回最终的结果
//
// 注意您的结果中 不应 包含任何括号
//
//
//
// 示例 1
//
// 输入s = "(abcd)"
//输出"dcba"
//
//
// 示例 2
//
// 输入s = "(u(love)i)"
//输出"iloveu"
//
//
// 示例 3
//
// 输入s = "(ed(et(oc))el)"
//输出"leetcode"
//
//
// 示例 4
//
// 输入s = "a(bcdefghijkl(mno)p)q"
//输出"apmnolkjihgfedcbq"
//
//
//
//
// 提示
//
//
// 0 <= s.length <= 2000
// s 中只有小写英文字母和括号
// 我们确保所有括号都是成对出现的
//
// Related Topics
// 👍 75 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//1190:反转每对括号间的子串
public class ReverseSubstringsBetweenEachPairOfParentheses {
public static void main(String[] args) {
//测试代码
Solution solution = new ReverseSubstringsBetweenEachPairOfParentheses().new Solution();
//dcba
System.out.println(solution.reverseParentheses("(abcd)"));
//iloveu
System.out.println(solution.reverseParentheses("(u(love)i)"));
//leetcode
System.out.println(solution.reverseParentheses("(ed(et(oc))el)"));
//apmnolkjihgfedcbq
System.out.println(solution.reverseParentheses("a(bcdefghijkl(mno)p)q"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseParentheses(String s) {
StringBuilder result = new StringBuilder();
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
String str = s.substring(i, i + 1);
if (")".equals(str)) {
StringBuilder temp = new StringBuilder();
while (!"(".equals(stack.peek())) {
temp.append(new StringBuilder(stack.pop()).reverse());
}
if ("(".equals(stack.peek())) {
stack.pop();
}
if (stack.isEmpty()) {
result.append(temp);
} else {
stack.push(temp.toString());
}
} else if (stack.isEmpty() && !"(".equals(str)) {
result.append(str);
} else {
stack.push(str);
}
}
if (stack.size() == 1) {
return stack.pop();
}
while (!stack.isEmpty()) {
result.append(new StringBuilder(stack.pop()).reverse());
}
return result.toString();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,42 @@
<p>给出一个字符串&nbsp;<code>s</code>(仅含有小写英文字母和括号)。</p>
<p>请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。</p>
<p>注意,您的结果中 <strong>不应</strong> 包含任何括号。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s = &quot;(abcd)&quot;
<strong>输出:</strong>&quot;dcba&quot;
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = &quot;(u(love)i)&quot;
<strong>输出:</strong>&quot;iloveu&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = &quot;(ed(et(oc))el)&quot;
<strong>输出:</strong>&quot;leetcode&quot;
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>s = &quot;a(bcdefghijkl(mno)p)q&quot;
<strong>输出:</strong>&quot;apmnolkjihgfedcbq&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= s.length &lt;= 2000</code></li>
<li><code>s</code> 中只有小写英文字母和括号</li>
<li>我们确保所有括号都是成对出现的</li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 75</li><li>👎 0</li></div>