您的位置:首页 > 其它

leetcode557——Reverse Words in a String III

2018-03-15 22:00 435 查看
题目大意:将给出的字符串反转,不改变句子中单词的顺序,只是将每个单词单独反转。
分析:字符串的应用。
代码:转载自http://blog.csdn.net/liuchuo/article/details/71156730
class Solution {
public:
string reverseWords(string s) {
string result = "";
stack<char> word;
int flag = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] != ' ')
word.push(s[i]);
if (s[i] == ' ' || i == s.length() - 1) {
if (flag == 1) result += " ";
while (!word.empty()) {
result += word.top();
word.pop();
flag = 1;
}
}
}
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: