leetcode 【 Reverse Words in a String 】python 实现

题目

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

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

代码:oj在线测试通过 Runtime: 172 ms

 1 class Solution:
 2     # @param s, a string
 3     # @return a string
 4     def reverseWords(self, s):
 5         words = s.split(‘ ‘)
 6
 7         if len(words) < 2 :
 8             return s
 9
10         tmp = ""
11         for word in words:
12             word = word.replace(‘ ‘,‘‘)
13             if word != "" :
14                 tmp = word + " " + tmp
15
16         tmp = tmp.strip()
17
18         return tmp

思路

把中间多个空格考虑去除了就OK了

最后用strip()函数把首位的空白去了

时间: 2024-10-13 01:24:29

leetcode 【 Reverse Words in a String 】python 实现的相关文章

[leetcode]Reverse Words in a String @ Python

原题地址:https://oj.leetcode.com/problems/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". 解题思路:这题很能体现python处理字符串的强大功能. 代码: class Solut

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q

LeetCode——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". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input

[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

[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——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters c

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

[LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

LeetCode: Reverse Words in a String &amp;&amp; Rotate Array

Title: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". https://leetcode.com/problems/reverse-words-in-a-string/ 思路:先将字符串全部逆转,在将每个单词逆转.具体实现,要注意空格,所以对字符串倒过来处理. cla

Leetcode: 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". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input