1190.反转每对括号间的字符串 Posted on 2020-04-28 Words count in article: 230 | Reading time ≈ 1 1190反转每对括号间的字符串下面是题目 下面是题目给出的代码123456class Solution {public: string reverseParentheses(string s) { }}; 解题思路和394字符串解码一样123456789101112131415161718192021222324252627class Solution {public: string reverseParentheses(string s) { stack<string> stk; string res; for (int i = 0; i < s.size(); i++) { //如果是字母,就加到res字符串里去 if(isalpha(s[i])){ res.push_back(s[i]); } else if (s[i]=='(') //如果是出现左括号,那么入栈+更新res { stk.push(res); res = ""; } else{ //如果是右括号,把现有的res反转,然后加给栈头 //出栈,更新res = 栈头 reverse(res.begin(),res.end()); stk.top()+=res; res=stk.top(); stk.pop(); } } return res; }}; reverse()函数的用法反转string123456int main() { string N; cin>>N; reverse(N.begin(), N.end()); cout<<N<<endl;} 反转字符数组1234567int main() { char a[101]; cin.getline(a,sizeof(a)); int m=strlen(a); reverse(a,a+m); puts(a);} -------------本文结束,感谢您的阅读------------- Post author: Jason Post link: https://jasonxqh.github.io/2020/04/28/1190-%E5%8F%8D%E8%BD%AC%E6%AF%8F%E5%AF%B9%E6%8B%AC%E5%8F%B7%E9%97%B4%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.