[LeetCode]题解(python):151-Reverse Words in a String

题目来源:

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



题意分析:

  给定一个字符串,里面包括多个单词,将这个字符串的单词翻转,例如"the sky is blue",得到"blue is sky the".



题目思路:

  首先获取每个单词,将单词记录到一个数组里面,然后翻转过来就行了。要处理的是前面和后面有空格的情况。



代码(python):

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s) == 0:
            return ""
        ans = []
        begin = i = 0
        mark = True
        while i < len(s):
            if s[i] != ‘ ‘and mark:
                begin = i
                mark = False
            if s[i] == ‘ ‘ and i > 0 and s[i - 1] != ‘ ‘:
                ans.append(s[begin:i])
                while i < len(s) and s[i] == ‘ ‘:
                    i += 1
                begin = i
            i += 1
        if s[-1] != ‘ ‘:
            ans.append(s[begin:len(s)])
        #print(ans)
        j = len(ans)
        if j == 0:
            return ""
        res = ans[j - 1]
        j -= 1
        while j > 0:
            res += ‘ ‘ + ans[j - 1]
            j -= 1
        return res

  

时间: 2024-10-19 14:53:37

[LeetCode]题解(python):151-Reverse Words in a String的相关文章

[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. click to show clarification.

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

151. Reverse Words in a String &amp;&amp; 61. Rotate List &amp;&amp; 189. Rotate Array

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". What constitutes a word?A sequence of non-space characters constitutes a word. Cou

[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

151. Reverse Words in a String Leetcode Python

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. linear time: 1. go through the

Leetcode 448. Find All Numbers Disappeared in an Array JAVA语言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. 题意:反转字符串中的单词,注意空格的处理!!! public 

leetcode 151. Reverse Words in a String --------- java

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. 倒序字符串. 注意使用BufferString,因为如果使用Stri

Leetcode 151. Reverse Words in a String 解题报告

[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. [Idea] 从左往右遍历一次,每当遇到空格暂停

LeetCode 151 reverse word 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. 这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2

Java for 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". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()