牛客網在線編程專題《劍指offer-面試題42》翻轉單詞順序列

我的個人微信公衆號:Microstrong

微信公衆號ID:MicrostrongAI

微信公衆號介紹:Microstrong(小強)同學主要研究機器學習、深度學習、計算機視覺、智能對話系統相關內容,分享在學習過程中的讀書筆記!期待您的關注,歡迎一起學習交流進步!

知乎主頁:https://www.zhihu.com/people/MicrostrongAI/activities

Github:https://github.com/Microstrong0305

個人博客:https://blog.csdn.net/program_developer

 題目鏈接:

https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3?tpId=13&tqId=11197&tPage=3&rp=3&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking

題目描述:

解題思路:

已經AC的代碼:

class Solution:

    def ReverseSentence(self, s):
        s_list = s.split(" ")
        s_list.reverse()
        return " ".join(s_list)

    def ReverseSentence2(self, s):
        words = s.split(" ")
        temp_sentence = []
        for i in range(len(words) - 1, -1, -1):
            temp_sentence.append(words[i])
        return " ".join(temp_sentence)


if __name__ == '__main__':
    sol = Solution()
    s = "student. a am I"
    print(sol.ReverseSentence2(s))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章