【LeetCode with Python】 151. Reverse Words in a String

题目

原题页面:https://leetcode.com/problems/reverse-words-in-a-string/
本文地址:http://leetcode.xnerv.wang/reverse-words-in-a-string/
题目类型:Medium
难度评价:String
类似题目:(M) Reverse Words in a String II

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 word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

分析

反转一句话中的单词。注意题中还要求去除首尾可能存在的空白字符,词与词之间也要去除多余空白字符,而只留一个空白。


代码

class Solution:

    # @param s, a string
    # @return a string
    def reverseWords(self, s):

        len_s = len(s)
        if len_s < 1:
            return s

        index_right = len_s - 1
        new_s = ""

        while index_right >= 0:
            while index_right >= 0 and ' ' == s[index_right]:
                index_right -= 1
            end_index = index_right
            while index_right >= 0 and ' ' != s[index_right]:
                index_right -= 1
            start_index = index_right + 1
            word = s[start_index : end_index + 1] if end_index >= 0 else ""
            new_s += (word + " ")

        return new_s.rstrip()

results matching ""

    No results matching ""