|
| 1 | +# 毎日一题 - 反转每对括号间的子串 |
| 2 | + |
| 3 | +## 信息卡片 |
| 4 | + |
| 5 | +* 时间:2019-09-23 |
| 6 | +* 题目链接:https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses |
| 7 | +* tag:`String` `Backtracking` |
| 8 | +## 题目描述 |
| 9 | +``` |
| 10 | +给出一个字符串 s(仅含有小写英文字母和括号)。 |
| 11 | +
|
| 12 | +请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。 |
| 13 | +
|
| 14 | +注意,您的结果中 不应 包含任何括号。 |
| 15 | +
|
| 16 | + |
| 17 | +
|
| 18 | +示例 1: |
| 19 | +
|
| 20 | +输入:s = "(abcd)" |
| 21 | +输出:"dcba" |
| 22 | +示例 2: |
| 23 | +
|
| 24 | +输入:s = "(u(love)i)" |
| 25 | +输出:"iloveu" |
| 26 | +示例 3: |
| 27 | +
|
| 28 | +输入:s = "(ed(et(oc))el)" |
| 29 | +输出:"leetcode" |
| 30 | +示例 4: |
| 31 | +
|
| 32 | +输入:s = "a(bcdefghijkl(mno)p)q" |
| 33 | +输出:"apmnolkjihgfedcbq" |
| 34 | + |
| 35 | +
|
| 36 | +提示: |
| 37 | +
|
| 38 | +0 <= s.length <= 2000 |
| 39 | +s 中只有小写英文字母和括号 |
| 40 | +我们确保所有括号都是成对出现的 |
| 41 | +``` |
| 42 | + |
| 43 | +## 参考答案 |
| 44 | + |
| 45 | +#### 思路 |
| 46 | + |
| 47 | +1. 对字符串中的字符遍历 |
| 48 | + |
| 49 | + |
| 50 | +2. 括号是有层次性的,所以这里用递归的方式处理内层的字符串,内层处理完成后回溯。返回内层有括号索引的下一个位置,以及括号内反转完成后的字符串。 |
| 51 | + |
| 52 | + |
| 53 | +3. 在2的递归过程中,原字符串也需要作为递归方法的参数传递进来 |
| 54 | + |
| 55 | + |
| 56 | +4. 递归方法的负责处理从当前位置开始到遇到对应的有括号之间的字符串,将其反转;若遇到新的左括号,创建临时的StringBuilder用于记录递归方法内反转的字符串,进入新的一层递归;递归完成后临时StringBuilder被填充并且是反转后的结果,append到上一层递归的StringBuilder中。 |
| 57 | + |
| 58 | + |
| 59 | +5. 递归回溯到最上层时,StringBuilder即为最终结果。 |
| 60 | + |
| 61 | + |
| 62 | +6. 更多代码细节可以关注下代码注释 |
| 63 | + |
| 64 | +#### 代码如下 |
| 65 | + |
| 66 | +```java |
| 67 | +package com.jinyang.algorithms.string; |
| 68 | + |
| 69 | +/** |
| 70 | + * Created by Zhang.Jinyang&Hardy on 2019/10/17. |
| 71 | + */ |
| 72 | +public class ReverseParentheses { |
| 73 | + |
| 74 | + public static void main(String[] args) { |
| 75 | + |
| 76 | + /** |
| 77 | + * 用例的类型 |
| 78 | + * (ab(cd)ef) |
| 79 | + * ab(cd) |
| 80 | + * ab(cd)ef |
| 81 | + * ((ab)c)def |
| 82 | + * abc(d(ef)) |
| 83 | + * */ |
| 84 | + String s = "ab((cd)ef)"; |
| 85 | + |
| 86 | + StringBuilder builder = new StringBuilder(); |
| 87 | + reverseParentheses(s, 0, builder, 0); |
| 88 | + System.out.println(builder.toString()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * leetcode 1190 |
| 93 | + * 耗时 1ms |
| 94 | + * 内存消耗 34.6M |
| 95 | + * */ |
| 96 | + static int reverseParentheses(String s, int index, StringBuilder stringBuilder, int leftCount) { |
| 97 | + |
| 98 | + |
| 99 | + //遍历字符串 |
| 100 | + while (index < s.length()) { |
| 101 | + /** |
| 102 | + * case 当前字符为'(' |
| 103 | + * leftCount++; 记录已遍历 但未 找到相对应的右括号 的左括号数目;这里每当当前字符为'(',leftCount+1 |
| 104 | + * case: index 为 0,第一个字符为'(' |
| 105 | + * index ++, 继续遍历 |
| 106 | + * case:index 不为0, 不是第一个字符 |
| 107 | + * 递归,index++,new stringBuilder(临时存放从当前'('到其相对应的')'里的字符,不包含'('和')'), leftCount |
| 108 | + * 递归后,new stringBuilder已被填充好 从当前'('到其相对应的')'里的字符 反转后的字符串;且返回参数为下一次要访问的字符下标index |
| 109 | + * leftCount --;因为进入递归后返回时已经将 当前'(' 与其响应的 ')' 内的字符反转,所以左括号数减1 |
| 110 | + * 递归前的 stringBuilder append 递归后,已反转的 new stringBuilder |
| 111 | + * 继续遍历. |
| 112 | + * */ |
| 113 | + if (s.charAt(index) == '(') { |
| 114 | + leftCount++; |
| 115 | + if (index != 0) { |
| 116 | + StringBuilder sTemp = new StringBuilder(); |
| 117 | + index = reverseParentheses(s, index + 1, sTemp, leftCount); |
| 118 | + leftCount--; |
| 119 | + stringBuilder.append(sTemp); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + index++; |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * case 当前字符为')' |
| 129 | + * 反转stringBuilder里的字符位置; |
| 130 | + * index++; |
| 131 | + * case:当前leftCount >1 那么当前是在递归过程中 |
| 132 | + * return index;//回溯 |
| 133 | + * case: 当前leftCount <=1 当前不是递归 |
| 134 | + * 继续遍历; |
| 135 | + * */ |
| 136 | + if (s.charAt(index) == ')') { |
| 137 | + stringBuilder.reverse(); |
| 138 | + index++; |
| 139 | + if (leftCount > 1) { |
| 140 | + return index; |
| 141 | + } else { |
| 142 | + continue; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /**当前字符不是'(' 或 ')' |
| 147 | + * 当前stringBuilder append 当前下标对应的字符 |
| 148 | + * index++ |
| 149 | + * 继续遍历 |
| 150 | + * */ |
| 151 | + stringBuilder.append(s.charAt(index)); |
| 152 | + index++; |
| 153 | + } |
| 154 | + return index; |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +## 优秀解答 |
| 165 | + |
| 166 | +>暂缺 |
0 commit comments