LeetCode OJ 系列之151 Reverse Words in a String --Python

来源:互联网 发布:左耳最后说了什么 知乎 编辑:程序博客网 时间:2024/06/06 07:31

Problem:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

Answer:

class Solution(object):    def reverseWords(self, s):        """        :type s: str        :rtype: str        """                tmp = s.split()        if len(tmp) == 0: return ""        results = tmp[-1]        for i in range(len(tmp)-2,-1,-1):            results += " "+tmp[i]        return results

0 0
原创粉丝点击