天天看点

Reverse Words in a String

Given an input string, reverse the string word by word.

For example,

Given s = "<code>the sky is blue</code>",

return "<code>blue is sky the</code>".

<a href="https://oj.leetcode.com/problems/reverse-words-in-a-string/">click to show clarification.</a>

Clarification:

What constitutes a word?

A sequence of non-space characters constitutes a word.

Could the input string contain leading or trailing spaces?

Yes. However, your reversed string should not contain leading or trailing spaces.

How about multiple spaces between two words?

Reduce them to a single space in the reversed string.

提交了好多遍,终于成功了,使用了sstream来分割字符串,使用栈来存放分割之后的字符串,然后出栈就是逆序了。。

C++实现代码:

运行结果:

Reverse Words in a String

继续阅读