当前位置:网站首页>1190. Invert the substring between each pair of parentheses
1190. Invert the substring between each pair of parentheses
2022-07-21 17:03:00 【baixiaofei567】
At the beginning of seeing this problem, I felt recursive or Stack do , The recursive version may not be written for a long time , It is not right , Or compromise to the stack .
What is recorded in the stack is the subscript of the left bracket , When you encounter the right parenthesis , Match the left bracket at the top of the stack with it and get out of the stack , Flip all the strings between these two subscripts , barring (), Including nothing . Finally, I'll go through tmp Then take it out () That's it
class Solution {
public:
string reverseParentheses(string s) {
// Recursive flip , Write a function , meet ( To get into , meet ) Quit
// Turn the front over , Plus what needs to be flipped later
// string res = "";
// for(int i = 0; i < s.length(); ++i){
// //substr Of c
// if(s[i] == '('){
// res += reverseParentheses(s.substr(i + 1));
// }
// else if(s[i] == ')'){
// reverse(res.begin(),res.end());
// return res;
// }
// else res += s[i];
// }
// return res;
// Bracket matching is still a priority stack better
// It is convenient to record subscripts , You can flip from the top of the stack to the current position
// What we save in the stack is ( The subscript
stack<int> sta;
string tmp = "";
for(int i = 0; i < s.length(); ++i){
tmp += s[i];
if(s[i] == '('){
sta.push(i);
}
else if(s[i] == ')'){
int start = sta.top();
sta.pop();// If the match is lost, it will be out of the stack
// The latter parameter is end Can't get , So there's no need to -1
reverse(tmp.begin() + start + 1, tmp.begin() + i);//+1 Because ( Don't add it
}
}
string res = "";
for(char ch : tmp){
if(ch != '(' && ch != ')'){
res += ch;
}
}
return res;
}
};
边栏推荐
- Scala exercises student scores case
- Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
- 字节顺序-大端/小端、big-endian/little-endian
- [program source code] information applet
- GAMES101图形学P12笔记(geometry3)
- Typescript函数扩展使用
- Selected papers of "deep neural network machine learning column"
- Network security module B of national vocational college skills competition "comprehensive penetration test of web security"
- Y72. Chapter IV Prometheus large factory monitoring system and practice -- Prometheus server installation and node exporter installation (III)
- (pc+wap) Zhimeng template air purification website
猜你喜欢
[solved] Splunk :Non-200/201 status_code=401; {“messages“:[{“type“:“WARN“,“text“:“call not properly
tried to access field com.mysql.cj.CharsetMapping.COLLATION_INDEX_TO_COLLATION_NAME from class io.de
FlinkCDC
Sliding of view
403. 青蛙过河
[Baoyan] - oral English skills in Baoyan summer camp
[Baoyan] - answer to open questions in Baoyan summer camp
OPENCV学习DAY2
c# winform 实际操作XML代码,包括创建、保存、查询、删除窗体演示
油猴插件使用
随机推荐
中国邮储银行 IT 集采:服务器、阵列、交换机、路由器、防火墙等
项目经理如何做好“老板”项目
exness:美指三连阴,欧元反弹消化加息预期
[guaranteed research] - sort out and share the problems existing in the previous school reexamination interview
初识无影云电脑
tried to access field com.mysql.cj.CharsetMapping.COLLATION_INDEX_TO_COLLATION_NAME from class io.de
How project managers do a good job in "boss" projects
Selected papers of "deep neural network machine learning column"
三丰智能唐海芹:为「三农」插上智慧翅膀,做智慧农业「引领者」| 镁客·请讲
这份 pip 使用小抄,要有全有多全!
「深度神经网络机器学习专栏」论文精选
Part 132 deletion in mapping
1723. 完成所有工作的最短时间
STM32 serial port screen learning
LeetCode 1260 二维网格迁移[数组] HERODING的LeetCode之路
Network security module B of national vocational college skills competition "comprehensive penetration test of web security"
ABAP grammatical basis
LNMP------PHP7安装
Fiddler设置断点
Redis内存模型讲解