20170225-leetcode-151-Reverse Words in a String
2017-02-25 20:57
375 查看
1.Description
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.
https://leetcode.com/problems/reverse-words-in-a-string/?tab=Description
解读
输入一个字符串,字符串中包含一些单词,单词之间由空格隔开,空格可能不止一个,并且前后也有可能有空格
要求,将单词反序排列,前后的空格去掉,单词之前如果本来有多个空格输出结果要变成一个空格
2.Solution
class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ slist=s.split(' ') str='' for item in reversed(slist): if item=='':continue str+=item.strip()+' ' return str.strip()
思路
把数据按照空格隔开
反着遍历
相关文章推荐
- Leetcode#151 Reverse Words in a String
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- LeetCode(151)Reverse Words in a String
- leetcode 151 —— Reverse Words in a String
- LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium
- [Leetcode 151, Medium] Reverse Words in a String
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- leetcode[151]:Reverse Words in a String
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- leetcode[151]Reverse Words in a String
- LeetCode: Reverse Words in a String [151]
- Leetcode 151:Reverse Words in a String
- Reverse Words in a String - LeetCode 151
- LeetCode151:Reverse Words in a String
- leetcode-151 Reverse Words in a String
- [LeetCode 151] Reverse Words in a String
- [leetcode 151]Reverse Words in a String
- [leetcode151] Reverse Words in a String
- LeetCode(151) Reverse Words in a String
- leetcode:151Reverse Words in a String java实现