leetcode 【 Reverse Words in a String 】python 实现
2014-12-27 00:12
525 查看
题目:
Given an input string, reverse the string word by word.
For example,
Given s = "
return "
代码:oj在线测试通过 Runtime: 172 ms
思路:
把中间多个空格考虑去除了就OK了
最后用strip()函数把首位的空白去了
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
class Solution: # @param s, a string # @return a string def reverseWords(self, s): words = s.split(' ') if len(words) < 2 : return s tmp = "" for word in words: word = word.replace(' ','') if word != "" : tmp = word + " " + tmp tmp = tmp.strip() return tmp
思路:
把中间多个空格考虑去除了就OK了
最后用strip()函数把首位的空白去了
相关文章推荐
- LeetCode 之 Reverse Words in a String — C 实现
- 【LeetCode with Python】 Reverse Words in a String
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- 151. Reverse Words in a String Leetcode Python
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode]Reverse Words in a String(using Python)
- python:leetcode之reverse words in a string
- leetcode 日经贴,python code -Reverse Words in a String
- [leetcode, python] Reverse Words in a String 反转字符串
- Leetcode - Python - Reverse Words in a String
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode]Reverse Words in a String @ Python
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- LeetCode:reverse words in a string---两种方法实现
- 【leetcode】Reverse Words in a String (python)