您的位置:首页 > 编程语言 > Python开发

[python] Lift is short, I need python!

2015-07-26 00:27 591 查看
Example 1: Reverse words in a String

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

For example,

Given s = "
the sky is blue
",

return "
blue is sky the
".

class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
if len(s)==0 :
return s
#reverse the whole string first
s=s[::-1]
sret=""
wordlist=s.split(' ')
for word in wordlist:
if sret!="" and word!="":
sret+=" "
word=word[::-1]
if word!="":
sret+=word
return sret
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: