Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
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.
题目分析:与传统的字符串翻转不同,该题目要求考虑删除字符串头尾和单词间的多余空格。
解题思路:1、先对整个字符串进行翻转,再对每个单词进行翻转,最后删除多余空格;
2、两次翻转后进行格式化输出;
3、先删多余空格,再进行两次翻转(和1基本相同)。
实现代码:
思路一: