当前位置:网站首页>1190. 反转每对括号间的子串 ●●
1190. 反转每对括号间的子串 ●●
2022-07-18 01:44:00 【chenyfan_】
1190. 反转每对括号间的子串 ●●
描述
给出一个字符串 s(仅含有小写英文字母和括号)。
请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。
注意,您的结果中 不包含任何括号。
示例
输入:s = “(ed(et(oc))el)”
输出:“leetcode”
解释:先反转子字符串 “oc” ,接着反转 “etco” ,然后反转整个字符串。
输入:s = “(u(love)i)”
输出:“iloveu”
解释:先反转子字符串 “love” ,然后反转整个字符串。
题解
本题要求按照从括号内到外的顺序进行处理。如字符串 (u(love)i),首先处理内层括号,变为 (uevoli),然后处理外层括号,变为 iloveu。
对于括号序列相关的题目,通用的解法是使用递归或栈。
1. 递归
遇到左括号则进入新的递归,处理括号内的内容。
- 时间复杂度: O ( n 2 ) O(n^2) O(n2),其中 n 为字符串的长度。栈的最大深度为 O(n),每一层处理的时间复杂度主要为反转的时间复杂度,为 O(n),因此总时间复杂度为 O ( n 2 ) O(n^2) O(n2)。
- 空间复杂度: O ( n ) O(n) O(n),其中 n 为字符串的长度。对于任意时刻,字符串中的任意一个字符至多只被栈中的一个位置包含一次。
class Solution {
public:
int idx = 0; // 全局变量,不同递归间的遍历索引一样
string reverseParentheses(string s) {
string ans; // 当前递归层的字符串
for(; idx < s.length(); ++idx){
if(s[idx] == '('){
// 左括号,新的递归层
++idx;
ans += reverseParentheses(s); // 字符串拼接,返回的时候idx指向了右括号的位置
}else if(s[idx] == ')'){
reverse(ans.begin(), ans.end()); // 右括号,反转当前层的字符串
break;
}else{
ans.push_back(s[idx]); // 字母
}
}
return ans;
}
};
2. 栈 模拟递归
从左到右遍历该字符串,使用字符串 str 记录当前层所遍历到的小写英文字母。对于当前遍历的字符:
- 如果是左括号,将 str 入栈中,并将 str 置为空,进入下一层;
- 如果是右括号,则说明遍历完了当前层,需要将 str 反转,返回给上一层。具体地,将栈顶字符串弹出,然后将反转后的 str 拼接到栈顶字符串末尾,并将结果赋值给 str。
- 如果是小写英文字母,将其加到 str 末尾。
注意到我们仅在遇到右括号时才进行字符串处理,这样可以保证我们是按照从括号内到外的顺序处理字符串。
- 时间复杂度: O ( n 2 ) O(n^2) O(n2),其中 n 为字符串的长度。栈的最大深度为 O(n),每一层处理的时间复杂度主要为反转的时间复杂度,为 O(n),因此总时间复杂度为 O ( n 2 ) O(n^2) O(n2)。
- 空间复杂度: O ( n ) O(n) O(n),其中 n 为字符串的长度。对于任意时刻,字符串中的任意一个字符至多只被栈中的一个位置包含一次。

class Solution {
public:
string reverseParentheses(string s) {
string str;
stack<string> strs;
for(int idx = 0; idx < s.length(); ++idx){
if(s[idx] == '('){
strs.push(str); // 入栈
str = ""; // 重置
}else if(s[idx] == ')'){
reverse(str.begin(), str.end()); // 反转
str = strs.top() + str; // 拼接
strs.pop();
}else{
str.push_back(s[idx]);
}
}
return str;
}
};
3. 预处理括号
将括号的反转理解为逆序地遍历括号:
- 第一步,我们向右移动到左括号,此时我们跳跃到该左括号对应的右括号(进入了更深一层);
- 第二到第三步,我们在括号内部向左移动(完成了更深层的遍历);
- 第四步,我们向左移动到左括号,此时我们跳跃到该左括号对应的右括号(返回到上一层);
- 第五步,我们在括号外向右移动(继续遍历)。
那么总结规律就是:
假设我们沿着某个方向移动,此时遇到了括号,那么我们只需要首先跳跃到该括号对应的另一个括号所在处,然后改变移动方向即可。
这个方案同时适用于遍历时进入更深一层,以及完成当前层的遍历后返回到上一层的方案。
在实际代码中,我们需要预处理出每一个括号对应的另一个括号所在的位置,这一部分我们可以使用栈解决。当我们预处理完成后,即可在线性时间内完成遍历,遍历的字符串顺序即为反转后的字符串。
- 时间复杂度: O ( n ) O(n) O(n),其中 n 为字符串的长度。预处理出括号的对应关系的序列的时间复杂度为 O(n),遍历字符串的时间复杂度同样为 O(n)。
- 空间复杂度: O ( n ) O(n) O(n),其中 n 为字符串的长度。栈的大小不会超过 n,以及我们需要 O(n) 的空间记录括号的对应关系。
class Solution {
public:
string reverseParentheses(string s) {
string ans;
int n = s.length();
vector<int> pair(n); // 括号索引对
stack<int> st;
for(int i = 0; i < n; ++i){
// 将一一对应的左右括号下标进行预处理
if(s[i] == '('){
st.push(i);
}else if(s[i] == ')'){
int l = st.top();
pair[l] = i;
pair[i] = l;
st.pop();
}
}
int idx = 0, step = 1; // step 表示遍历的方向
while(idx < n){
if(s[idx] == '(' || s[idx] == ')'){
// 遇到括号
idx = pair[idx]; // 索引跳转
step = -step; // 遍历方向反转
}else{
ans.push_back(s[idx]); // 加入字符
}
idx += step; // 移动索引
}
return ans;
}
};
边栏推荐
- MySQL - adjust column constraints
- [dry goods] how much do you know about MySQL infrastructure design?
- MySQL - 表索引概述
- Solve the problem of "license manager error -8" after matlab installation (the personal test is valid)
- Ch549/ch548 learning notes 2 - system clock
- MySQL 正则表达式
- 关于QProcess的使用问题解释
- Ch549/ch548 Learning Notes 6 - read chip ID
- Is it free to open an account online with flush software for stock speculation? Is it safe to open an account?
- ["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
猜你喜欢

Okaleido or get out of the NFT siege, are you optimistic about it?

Conscience products produced by Tencent? Detailed experience of translation function

JVM personal learning notes

Basic use of anaconda and its use in pychart

软件测试周刊(第80期):当你想倾诉的话语已经涌到了舌尖,但是把那些话憋回去的瞬间,从那个瞬间起,你就成为了大人。

Chapter 4: emerging, class instantiation strategy with constructor based on cglib

Summary of common problems of SolidWorks assembly (updated at any time)

CH549/CH548学习笔记5 - SPI主模式
![[foundation of deep learning] how to understand the channel in convolutional neural network](/img/07/283c148fd470cdac0c7538f6d1ce50.png)
[foundation of deep learning] how to understand the channel in convolutional neural network

Ugui source code analysis - clipperregistry
随机推荐
Openresty Lua resty lrucache cache
同花顺软件炒股线上开户收费吗?开户安全吗?
支付宝上怎么买基金,安全吗
UGUI源码解析——RectMask2D
MySQL - adjust column constraints
Ch549/ch548 learning notes 2 - system clock
软件测试周刊(第80期):当你想倾诉的话语已经涌到了舌尖,但是把那些话憋回去的瞬间,从那个瞬间起,你就成为了大人。
同花顺软件线上开户免费吗?开户安全吗?
Read the paper: temporary graph networks for deep learning on dynamic graphs
openresty ngx_lua共享内存
对象内存布局和synchronized锁升级
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Cause analysis of the red light of the server and soft switch status on the Vos client
[foundation of deep learning] how to understand the channel in convolutional neural network
线上基金开户是否安全呢?在线求答案
Conscience products produced by Tencent? Detailed experience of translation function
深度学习环境配置Pytorch
CH549/CH548學習筆記9 - USB Device端點處理過程
[MCU simulation project] advertising lamp (Proteus schematic +keil code)
MySQL 正则表达式