您的位置:首页 > 其它

[LeetCode]Reverse Words in a String

2014-05-11 10:19 375 查看
Given an input string, reverse the string word by word.

For example,

Given s = "the sky is blue",

return "blue is sky the".

class Solution {
public:
void reverseWords(string &s) {
std::stringstream ss(s);
std::string word;
std::vector< std::string> temp;
while (ss>>word)
{
temp.push_back(word);
temp.push_back(" ");
}
s = "";
for (int i = temp.size() - 2; i >= 0; i--)
{
s += temp[i];
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: