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. Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'as3'

    执行更新时的出错信息 Whitelabel Error Page This application has no explicit mapping for /error, so you are see ...

  2. cocos2dx 在windows下开启console

    cocos2dx 3.10 1.在main函数中写入代码 AllocConsole(); freopen("CONIN$", "r", stdin); freo ...

  3. English trip -- Phonics 5 元音字母 o

    Vowel 元音 元音 O Consonant 辅音 清辅音   h    wh 浊辅音  m    wh  n   ng   y oa:[əʊ]  # 字母本身音 coat boat load co ...

  4. 百度安卓SDK秘钥Key错误

    下载官方安卓地图demo,输入报名和sha1申请AK,发现key错误 构建的时候要指定生成的key 安卓定位BaiduLocDemo出现aapt.exe finished with non-zero ...

  5. Prime Gift CodeForces - 912E (中途相遇)

    链接 大意:求素因子只含给定素数的第k大数 先二分答案转为判定x是第几大, 然后分两块合并即可, 按奇偶分块可以优化一下常数 #include <iostream> #include &l ...

  6. Multi-target tracking by Lagrangian relaxation to min-cost network flow

    Multi-target tracking by Lagrangian relaxation to min-cost network flow high-order constraints min-c ...

  7. 河南省第十一届ACM大学生程序设计竞赛

    nyoj-1365-山区修路 内存限制:128MB 时间限制:3000ms 特判: No通过数:4 提交数:4 难度:3 题目描述: SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间, ...

  8. 字符串 dfs

    1222: FJ的字符串 [水题] 时间限制: 1 Sec 内存限制: 128 MB 提交: 52 解决: 9 状态 题目描述 FJ在沙盘上写了这样一些字符串: A1  =  “A” A2  =  “ ...

  9. Coconuts, Revisited(递推+枚举+模拟)

    Description The short story titled Coconuts, by Ben Ames Williams, appeared in the Saturday Evening ...

  10. java 判断字符串IP合法性以及获取IP的数值形式

    /** * 计算传入的IP地址的数字IP*/ public static long getIpNum(String ip) { long ipNum = 0; if (StringUtils.isNo ...