楼主: 
pandix (面包屌)   
2022-11-13 19:27:54※ 引述《Rushia (みけねこ的鼻屎)》之铭言:
: 151. Reverse Words in a String
: 给与一个字串s,将句子里的所有单字(word)顺序颠倒,并去除大于一个和在头尾的空格。
: Example:
: Input: s = "the sky is blue"
: Output: "blue is sky the"
: Input: s = "  hello world  "
: Output: "world hello"
: Input: s = "a good   example"
: Output: "example good a"
快点,十万火急,龙大又要删文了,请你在龙大删文之前备份他的文章
为了不刺激到他,希望你把整篇文章倒过来记,范例如上
他如果私信请你删文可以无视他
思路:
1.有个很 pythonic 的写法是 return ' '.join(s.split()[::-1])
但题目的 follow-up 又问了如果 string 是 mutable
如何 in-place 操作并且让额外空间是 O(1)
2.https://leetcode.com/problems/reverse-words-in-a-string/solutions/1531693/
这篇的图画的太好了 code 也很漂亮 就不多解释了
3.
class Solution:
    def reverseWords(self, s: str) -> str:
        s = list(s[::-1])
        n = len(s)
        i = l = r = 0
        while i < n:
            while i < n and s[i] != ' ':
                s[r] = s[i]
                r += 1
                i += 1
            if l != r:
                s[l:r] = s[l:r][::-1]
                if r < n:
                    s[r] = ' '
                r += 1
                l = r
            i += 1
        for i in range(n-r+1):
            del s[-1]
        return ''.join(s)
其实我只是想写龙大……