[python] Lift is short, I need python!

来源:互联网 发布:淘宝沙滩裙桑蚕丝 编辑:程序博客网 时间:2024/04/28 12:33

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





0 0
原创粉丝点击