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
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 实现的更多相关文章
- [leetcode]Reverse Words in a String @ Python
原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [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 ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- leetcode:Reverse Words in a String【Python版】
class Solution: # @param s, a string # @return a string def reverseWords(self, s): ss = s.split(&quo ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
随机推荐
- Spring MVC + Thymeleaf
参考网址: https://www.cnblogs.com/litblank/p/7988689.html 一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应 ...
- Array负载均衡控制器(vAPV)
平台: freebsd 类型: 虚拟机镜像 软件包: apache python basic software load balance network infrastructure slb ssl ...
- VMware虚拟机配置文件(.vmx)损坏修复
我的虚拟机为VM14 装的ubuntu14.04server版 遇到ubuntu打不开,上网查阅了博客写的解决办法,尝试并解决了,以下分享个人心得: 首先进入虚拟机中系统安装的位置 查看日志文件 ...
- Linux常用命令-1
内部命令:属于Shell解释器的一部分(已调入内存) 外部命令:独立于Shell解释器之外的程序文件(在磁盘上) 获得命令帮助 1)内部命令help 查看Bash内部命令的帮助信息 2)命令的“--h ...
- 实战:ADFS3.0单点登录系列-集成SharePoint
这是本系列第四篇了,终于轮到SharePoint上场了,但是本文不会过多讲解SharePoint安装等话题,而是直入主题,讲解如何进行配置,让其于ADFS配合完成SSO的工作. 注意:本文使用的Sha ...
- java Vamei快速教程07 包
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经写了一些Java程序.之前的每个Java程序都被保存为一个文件,比如Tes ...
- Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(1)
下周一出差宁波了,周六日就折腾点视频: 跟着视频教程开发,不过开发环境换linux,上月找工作,某个吉祥物是松鼠的公司要求用linux开发,没用过的,连面试机会都不给,极其高冷:好吧,咱就试试,用li ...
- 求和VII
问题 K: 求和VII 时间限制: 2 Sec 内存限制: 256 MB提交: 422 解决: 53[提交] [状态] [讨论版] [命题人:admin] 题目描述 master对树上的求和非常感 ...
- 2018.6.12 Oracle问题
ORA-01950: 对表空间 'USERS' 无权限 创建新的用户时,要指定default tablespace,否则它会把system表空间当成自己的缺省表空间.这样做是不提倡的.估计原来创建某个 ...
- Python02 变量
变量 因为Python是弱变量类型编程语言,所以变量赋值不需要类型声明. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. 变量 ...