leetcode:Reverse Words in a String【Python版】

 1 class Solution:
 2     # @param s, a string
 3     # @return a string
 4     def reverseWords(self, s):
 5         ss = s.split(" ")
 6         ss = filter(lambda x:x!="",ss)
 7         l = len(ss)
 8         for i in range(l/2):
 9             ss[i],ss[l-1-i] = ss[l-1-i],ss[i]
10         s = (" ").join(ss)
11         return s

为了去掉''字符,尝试了好几种方法,最后还是用filter函数将其过滤掉了。

posted @ 2014-10-15 23:30  ZH奶酪  阅读(552)  评论(0编辑  收藏  举报