Given an input string, reverse the string word by word.
For example,
Given s = "
return "
Given s = "
the sky is blue
",return "
blue is sky the
".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
For C programmers: Try to solve it in-place in O(1) space.
思路:
1. 利用String的split函数,生成包含多个String的数组,注意其中会有空字符串。
2. 从后往前,利用StringBuilder建立反向字符串,注意检查不是空字符串,每次添加空格。
3. 如果StringBuilder为空返回空字符串,否则返回substring(0, sb.length()-1)
4. 输入字符串为空是唯一的特殊情况。
No comments:
Post a Comment