Skip to content

Commit 6a3dc7e

Browse files
zhangjinyangazl397985856
authored andcommitted
feat: 每日一题2019-09-23 (azl397985856#211)
* 每日一题 2019-10-11 * 去掉分析步骤中的推敲错误 * 修改格式问题,代码高亮,39588数学公式区域去掉了 * 对原 $$ 数学公式区域部分 添加了文字解释 * 每日一题20190923 * 补充添加标签,在README中添加链接
1 parent 188f0db commit 6a3dc7e

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

daily/2019-09-23.md

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
>暂缺

daily/2019-10-11.md

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
1. 由集合中元素的特点可以联想到 **二进制**。如果将集合中的所有元素都用二进制来表示的话:
2222

23+
1 等于 2的0次方 等于 二进制的 1
24+
2 等于 2的1次方 等于 二进制的 10
25+
4 等于 2的2次方 等于 二进制的 100
26+
...
27+
即:
2328
1=2^0=(1)2; 2=2^1=(10)2; 4 = 2^2 = (100)2;..... .**(等号最后的数都是二进制表示法)**
2429

2530
集合中的每一个元素都可以表示为 首位为 1 其他位为 0 的二进制数。

daily/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,8 @@ tag: `DP` `Bit`
276276

277277
时间: 2019-10-11
278278

279+
### [1190.反转每对括号间的子串](./2019-09-23.md)
279280

281+
tag: `String` `Backtracking`
282+
283+
时间: 2019-09-23

0 commit comments

Comments
 (0)