[LeetCode&Python] Problem 557. Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
My first solution:
class Solution:
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
answer=''
smallstr='' for c in s:
if c==' ':
smallstr=smallstr[::-1]
answer=answer+smallstr+' '
smallstr=''
else:
smallstr=smallstr+c return answer+smallstr[::-1
A much shorter solution:
class Solution:
def reverseWords(self, s):
"""
:type s: str
:rtype: str
""" return ' '.join(i[::-1] for i in s.split(' '))
[LeetCode&Python] Problem 557. Reverse Words in a String III的更多相关文章
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
随机推荐
- [设计模式][C++]单例模式
参考:http://blog.csdn.net/hackbuteer1/article/details/7460019 单例模式意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有 ...
- URAL 1183 Brackets Sequence
URAL 1183 思路:区间dp,打印路径,详见http://www.cnblogs.com/widsom/p/8321670.html 代码: #include<iostream> # ...
- Java多线程之内存可见性和原子性:Synchronized和Volatile的比较
Java多线程之内存可见性和原子性:Synchronized和Volatile的比较 [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article ...
- ORACLE COMMENTON 使用
oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON { TABLE [ schema. ] { table | view } | COLUMN [ s ...
- 3-29 params的理解; Active Model Errors; PolymorphicRoutes 多态的路径; ::Routing::UrlFor
params的理解和作用: http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require A ...
- Python装饰器、生成器、内置函数、json
这周学习了装饰器和生成器,写下博客,记录一下装饰器和生成器相关的内容. 一.装饰器 装饰器,这个器就是函数的意思,连起来,就是装饰函数,装饰器本身也是一个函数,它的作用是用来给其他函数添加新功能,比如 ...
- _beginthreadex()和CreateThread()的区别
在本例子中我们使用——beginThreadex这个函数,它和createThread的区别是: 为了方便管理,我么在使用该函数的时候可以把它的线程函数作为类成员,这也就需要在类中把该函数变成静态函数 ...
- 使用API更新供应商名称及曾用名
原文地址 更新供应商名称 EBS R12 (12.1.3) 更新供应商名称或替代供应商名称不能直接使用 pos_vendor_pub_pkg.update_vendor(p_vendor_rec =& ...
- OAF 动态创建组件以及动态绑定属性
在开发中,我们遇到以下一个需求. 一个表格左侧有5列是固定存在的,右侧有N列是动态生成的,并且该N列中第一列可输入,第二列是不可编辑的,但是是数字,如果小于0,那么就要显示为红色,重点标识出来. 首先 ...
- 常见MIME类型例表
常见MIME类型例表: 序号 内容类型 文件扩展名 描述 1 application/msword doc Microsoft Word 2 application/octet-stream bin ...