题目

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 】python 实现的更多相关文章

  1. [leetcode]Reverse Words in a String @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...

  2. 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 arithm ...

  3. [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:Giv ...

  4. [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 ...

  5. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  6. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  7. [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 sti ...

  8. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  9. leetcode:Reverse Words in a String【Python版】

    class Solution: # @param s, a string # @return a string def reverseWords(self, s): ss = s.split(&quo ...

  10. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

随机推荐

  1. 设置mapcontrol的鼠标样式

    http://blog.itpub.net/14999074/viewspace-586515/ mapcontrol的鼠标样式 this.axMapControl1.MousePointer=esr ...

  2. 【转】【C++】【MFC】各种数据类型大小

    *原文地址:http://blog.csdn.net/xuexiacm/article/details/8122267 /*运行结果分析: 以上结果已经很明白了,一下补充说明几点: 概念.整型:表示整 ...

  3. 开发Maven插件

    Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...

  4. COGS 1043. [Clover S2] Freda的迷宫

    ★   输入文件:mazea.in   输出文件:mazea.out   简单对比时间限制:1 s   内存限制:128 MB Freda 的迷宫 (mazea.pas/.c/.cpp) 题目叙述 F ...

  5. 汇编:jmp系列跳转指令总结

    助记方法: J:跳转C: 进位位置位N: 否S: 符号位置位o: 溢出位置位Z: 零标志位置位E: 等于P:奇偶位置位A: AboveB: BelowL: Less (Little的比较级)G: Gr ...

  6. UVA 1213 - Sum of Different Primes(递推)

    类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. ...

  7. python_6_if

    _username='qi' _password='abc123' username=input("username:") password=input('password:') ...

  8. python-kafka源码解析之socketpair

    socket基本操作包括:socket()函数创建socket文件描述符,唯一标识一个socket.bind()函数,将ip:port和socket绑定listen()函数来监听这个socket,假如 ...

  9. 第十五篇、OC_同一个View实现两个手势响应

    #pragma mark-UIGestureRecognizerDelegate Methods // 只要实现这个方法,就可以实现两个手势同时响应 - (BOOL)gestureRecognizer ...

  10. 【转】 bind1st bind2nd的使用

    以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函 ...