Leetcode: reverse words in a string

来源:互联网 发布:神户制钢造假 知乎 编辑:程序博客网 时间:2024/05/22 00:45

http://oj.leetcode.com/problems/reverse-words-in-a-string/


class Solution:
    # @param s, a string
    # @return a string
    def reverseWords(self, s):
        return ' '.join(s.split()[::-1])


这道题目挺简单,但有一点值得学习,那就是reverse string(list)的不同方式效率并不一样。

详见:http://stackoverflow.com/questions/931092/reverse-a-string-in-python

其中string/list[::-1]是最快的一个。但用其他的方式也能通过OJ。

0 0