class Solution {
public:
string ReverseSentence(string str) {
string res = str;
if(str == "")
return res;
int start = 0;
int end = 0;
int cur = 0;
while(cur++ < res.size())
{
if(cur == res.size() || res[cur] == ' ')
{
end = cur - 1;
while(start < end)
swap(res[start++], res[end--]);
start = end = cur + 1;
}
}
start = 0;
end = res.size() - 1;
while(start < end)
swap(res[start++], res[end--]);
return res;
}
};
本文介绍了一种使用C++实现字符串逆序的方法。通过定义一个Solution类,并在其中实现ReverseSentence成员函数,该函数接收一个字符串参数,返回逆序后的字符串。在函数中,首先对字符串中的每个单词进行逆序处理,然后再对整个字符串进行逆序,从而达到句子逆序的效果。
1998

被折叠的 条评论
为什么被折叠?



