Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
<pre name="code" class="python">class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
s = s.strip().split(' ')
s = s[::-1]
result = []
lens = len(s)
for i in xrange(lens):
if s[i] != '':
result.append(s[i])
result = ' '.join(result)
return result
本文介绍了一种将输入字符串按单词进行反转的方法。通过Python实现,首先去除字符串两端的空白字符,并将其分割成单词列表,接着反转该列表并组合为新的字符串返回。
567

被折叠的 条评论
为什么被折叠?



