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

leetcode 151 Reverse Words in a String (python)

2016-07-18 18:33 513 查看
leetcode 151   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".

python代码:

class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
rs=s[::-1] #将整个字符串反转
l=rs.split() #将反转后的字符串通过split()函数进行分割,产生的单词发在列表l中
ls=[word[::-1] for word in l] #使用列表解析,反转列表l中的每一个单词
return ' '.join(ls)  #使用join()函数,将列表ls中的单词,通过空格连接成一个字符串


(完)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: