牛客网在线编程专题《剑指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))

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页