翻转单词的顺序-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