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 ...
随机推荐
- WPF中的StackPanel、WrapPanel、DockPanel(转)
一.StackPanel StackPanel是以堆叠的方式显示其中的控件 1.可以使用Orientation属性更改堆叠的顺序 Orientation="Vertical" 默认 ...
- C# asp.net mvc 注解验证
看代码,看注解,看懂了单词,没看懂意思. 今日只能专攻一下这项特性. 1.Remote 在看这个例子的时候 ,看了JsonResult 以及 JsonRequestBehavior.AllowGet解 ...
- Euerka环境搭建
机器环境 windows10,IntelliJ IDEA 配置host 单节点Eureka 一.pom文件 <?xml version="1.0" encoding=&quo ...
- IOS CALayer基本使用 (图层)
● 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层(CALayer) ● 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView 的l ...
- 2018.5.19 Oracle数据操作和管理表的综合练习
--作业一.使用自己的用户登录,完成如下操作,并且创建5条测试数据 -- 创建学生表(stu),字段如下: -- 学号(stuID) -- 姓名(stuName) -- 性别(stuSex) -- 入 ...
- python_18_三元运算
# result=值1 if 条件 else 值2 如果条件为真:result=值1,否则result=值2. a,b,c=1,3,5 d=a if b>c else c print(d)
- PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)
http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第三节
原文链接 第三节:错误处理和全局内存性能局限 恭喜!通过对CUDA(Compute Unified DeviceArchitecture,即计算统一设备架构的首字母缩写)系列文章第一节和第二节,您现在 ...
- C#继承机制 多级继承
一些面向对象语言允许一个类从多个基类中继承,而另一些面向对象语言只允许从一个类继承,但可以随意从几个接口或纯抽象类中继承. 只有C++支持多级继承,许多程序员对此褒贬不一.多级继承常会引起继承来的类之 ...
- jQuery入门第一天-(一个菜鸟的不正经日常)
jQuery的初步认识 菜鸟Q1:什么是jQuery? jQuery就是一个JavaScript函数库,没什么 特别的. 菜鸟Q2:jQuery能做什么?jQuery是做什么的? jQuery本身就是 ...