1.将字符串中的单词变为数组,数组从两头开始交换,最后数组变回字符串输出。

# -*- coding:utf-8 -*-
class Solution:
    def ReverseSentence(self, s):
        # write code here
        if not s:
            return s
        words_list = s.split(' ')
        start = 0
        end = len(words_list) - 1
        while start < end:
            words_list[start], words_list[end] = words_list[end], words_list[start]
            start += 1
            end -= 1
        return ' '.join(words_list)