186. Reverse Words in a String II

https://leetcode.com/problems/reverse-words-in-a-string-ii/#/description

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.

The input string does not contain leading or trailing spaces and the words are always separated by a single space.

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

Could you do it in-place without allocating extra space?

Sol :

class Solution:
    # @param s, a list of 1 length strings, e.g., s = [‘h‘,‘e‘,‘l‘,‘l‘,‘o‘]
    # @return nothing
    def reverseWords(self, s):

        # Time O(n) Space O(1)
        # We first reverse every single word and then reverse the string from start to end.

        def reverse(i,j):
            while 0 <= i < j < len(s):
                s[i], s[j] = s[j], s[i]
                i, j = i + 1, j - 1

        # ex. s = [‘t‘, ‘h‘, ‘e‘, ‘ ‘, ... , ‘b‘, ‘l‘, ‘u‘, ‘e‘]
        s.append(" ")
        start = 0
        for i, v in enumerate(s):
            # reverse chars in every word
            # ex. s = [‘e‘, ‘h‘, ‘t‘, ... , ‘e‘, ‘u‘, ‘l‘, ‘b‘]
            if v == " ":
                reverse(start, i - 1)
                start = i + 1

        s.pop()
        # reverse each char in s
        # ex. s = [‘b‘, ‘l‘, ‘u‘, ‘e‘, ... , ‘t‘, ‘h‘, ‘e‘]
        reverse(0, len(s) - 1)
时间: 2024-10-03 17:28:40

186. Reverse Words in a String II的相关文章

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. For example,Given s =

leetcode 186: Reverse Words in a String II

Total Accepted: 421 Total Submissions: 1404 Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separat

186. Reverse Words in a String II 翻转有空格的单词串 里面不变

[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l

[LeetCode] Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. For example,Given s =

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

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. Clarification: What constitutes a

[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns