题目

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. 禁止ASP.NET MVC模型绑定时将空字符串绑定为null

    为model添加[DisplayFormat(ConvertEmptyStringToNull = false)] [Display(ResourceType = typeof(AppStrings) ...

  2. centos6.5_64bit安装Redis3.2.8

    一.去官网下载最新稳定版 https://redis.io/   二.打开redis需要的端口 /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEP ...

  3. mybatis-映射器的CRUD

    设计步骤:model.mapper.dao.service.junit单元测试.log4j日志 项目和之前的一样在此只是创建了test和修改了mapper 1.修改映射 1.1修改接口 package ...

  4. cesium加载shp格式数据

    方法一: shp格式转换为GeoJson格式并加载 首先注意shp的坐标系,要转换为WGS84,使用arcgis或QGIS 工具:http://mapshaper.org/: 注意:export时,输 ...

  5. 如何解析比特币中的交易原始数据rawData

    交易数据结构 有关交易的详细信息可以查看比特币的wiki网站:Transaction TxBinaryMap: 原始图片地址 交易的结构表格(Transaction): 示例数据 以一个正式网络的一笔 ...

  6. 【洛谷3950】部落冲突(LCT维护连通性)

    点此看题面 大致题意: 给你一棵树,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 有一道类似的题目:[BZOJ2049][SDOI2008] Cave 洞穴勘测. ...

  7. 手机上如何远程控制Linux服务器?

    这里介绍3个手机软件,分别是JuiceSSH.Termius和Termux,这3个软件都可以实现远程控制Linux服务器(相当于手机SSH客户端),而且使用起来都非常方便,下面我简单介绍一下这3个软件 ...

  8. 复习C++_基础、函数、数组、字符串

    程序的开发过程 程序 源程序:用源语言写的,有待翻译的程序: 目标程序:源程序通过翻译程序加工以后生成的机器语言程序: 可执行程序:连接目标程序以及库中的某些文件,生成的一个可执行文件,例如Windo ...

  9. CUDA实现数组倒序

    数组倒序,将在主机上初始化的数组传输到设备上,然后用CUDA并行倒序,此时在全局内存上操作,再将结果返回到主机并验证. #include <stdio.h> #include <as ...

  10. Echarts样式

    Echarts设置样式如下 <div id="main" style="width: 250px;height:200px;"></div&g ...