1190:反转每对括号间的子串
This commit is contained in:
parent
6f0e01e7d8
commit
b5dd7542d4
@ -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)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
<p>给出一个字符串 <code>s</code>(仅含有小写英文字母和括号)。</p>
|
||||||
|
|
||||||
|
<p>请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。</p>
|
||||||
|
|
||||||
|
<p>注意,您的结果中 <strong>不应</strong> 包含任何括号。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>s = "(abcd)"
|
||||||
|
<strong>输出:</strong>"dcba"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>s = "(u(love)i)"
|
||||||
|
<strong>输出:</strong>"iloveu"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>s = "(ed(et(oc))el)"
|
||||||
|
<strong>输出:</strong>"leetcode"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>s = "a(bcdefghijkl(mno)p)q"
|
||||||
|
<strong>输出:</strong>"apmnolkjihgfedcbq"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>0 <= s.length <= 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>
|
Loading…
Reference in New Issue
Block a user