1. 程式人生 > >劍指offer 44. 翻轉單詞順序列

劍指offer 44. 翻轉單詞順序列

原題

牛客最近來了一個新員工Fish,每天早晨總是會拿著一本英文雜誌,寫些句子在本子上。同事Cat對Fish寫的內容頗感興趣,有一天他向Fish借來翻看,但卻讀不懂它的意思。例如,“student. a am I”。後來才意識到,這傢伙原來把句子單詞的順序翻轉了,正確的句子應該是“I am a student.”。Cat對一一的翻轉這些單詞順序可不在行,你能幫助他麼?

Reference Answer

思路分析

先分割單詞,再逆序變化,pass。

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