翻转单词的顺序-python

思路:用到了split join等内置函数,之后再补充不用内置函数的方法吧

# -*- coding:utf-8 -*-
class Solution:
    def ReverseSentence(self, s):
        # write code here
        if s == "":
            return ""
        out = s.split()[::-1]
        if out == []:
            return s
        else:
            return " ".join(out)
                

方法2:

class Solution:
    def ReverseSentence(self, s):
        return " ".join(s.split()[::-1]) if s.strip() != "" else s
posted @ 2019-08-11 10:05  Dolisun  阅读(572)  评论(0编辑  收藏  举报