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的更多相关文章

  1. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  2. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  3. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

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

  5. 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 ...

  6. 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 ...

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

  8. 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 ...

  9. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. NetCat教程

    NetCat by Jian Lee 简介 使用 隐藏命令行参数 正/反向域名解析 参数详解 案例 监听端口(制作蜜罐) 端口扫描 ftp 服务器 两台服务器文件校验 使用注意 简介 使用 最简单的使 ...

  2. Beta冲刺

    第一天 日期:2018/6/24 1 今日完成任务情况. 妥志福.牛瑞鑫: 完成任务:数据库设计完成数据导入成功 王胜海.马中林: 完成任务:代码规范检查 董润园.邓英蓉: 完成任务:平台基本功能黑盒 ...

  3. 如何学好Java大数据并快速提升

    Java作为一种最流行的,最热门的编程语言,可以说它无处不在,目前全球有着数十亿的设备正在运行着Java,很多服务器程序都是用Java编写,用以处理每天超过数以千万的数据.无论是手机软件.手机Java ...

  4. 在ANE插件中启动AIR开发的Android应用

    在Android原生应用开发中,启动一个应用非常容易: Intent __intent = new Intent(this, YourAppActivity.class); startActivity ...

  5. Vue.js 响应式原理

    1. Vue2.x 基于 Object.defineProperty 方法实现响应式(Vue3 将采用 Proxy) Object.defineProperty(obj, prop, descript ...

  6. ASCII 可打印字符与控制字符

    2017-08-16 21:29:30 基本的 ASCII 字符集共有 128 个字符,其中有 95 个可打印字符,包括常用的字母.数字.标点符号等,另外还有 33 个控制字符.标准 ASCII 码使 ...

  7. SGE:qsub/qstat/qdel/qhost 任务投递和监控

    参考: Oracle Grid Engine qsub命令 SGE - qsub使用范例 SGE作业基本用法 qsub是最为稳定的底层任务投递系统,就是把一个脚本投递到集群的计算节点上运行. 注意,只 ...

  8. Jersey 2.x JDK 上的客户端应用

    如应用是运行在 JDK 上的话,你只需要使用 JAX-RS 中的客户端部分就可以了,这个根据你使用的客户端有所调整. 这里有一系列的模块是可以供你使用的,例如 grizzly 或 apache 或 j ...

  9. After reading a picture than out a picture

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  10. Eclipse修改已存在的SVN地址

    1.Window---->Show View---->Other...