[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 ...
随机推荐
- const 学习笔记
#include<stdlib.h> #include<iostream> using namespace std; int main(){ // const 仅仅起到是否为常 ...
- JS中Ajax的同步和异步
ajax同步 : 意味着此时请求Server后,JS代码不再继续执行,等待Server返回后才继续往下执行. ajax异步 : 意味着此时请求Server后,JS代码继续执行,不管Server什么时候 ...
- Codeforces 535C - Tavas and Karafs
535C - Tavas and Karafs 思路:对于满足条件的r,max(hl ,hl+1 ,hl+2 ,......,hr )<=t(也就是hr<=t)且∑hi<=t*m.所 ...
- C# DataTable按指定列排序
C#提供的内置对象DataTable功能特别的强大,如果我们需要对DataTable中的某一列进行排序怎么处理呢,具体代码如下: DataTable dt = new DataTable(); dt. ...
- 20170719xlVBASmartIndent
Public Sub SmartIndenterProcedure() Dim OneComp As VBComponent Dim StartLine As Long, EndLine As Lon ...
- Confluence 6 权限设置
备注:当 '外部用户管理' 权限没有被选择的时候,你仅可以指派 LDAP 用户到本地用户组中. 只读(Read Only) 从你目录服务器上获得 LDAP 用户,用户组只能通过你的目录服务器进行修改. ...
- 『cs231n』无监督学习
经典无监督学习 聚类 K均值 PCA主成分分析 等 深度学习下的无监督学习 自编码器 传统的基于特征学习的自编码器 变种的生成式自编码器 Gen网络(对抗式生成网络) 传统自编码器 原理 类似于一个自 ...
- 『科学计算』通过代码理解线性回归&Logistic回归模型
sklearn线性回归模型 import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model de ...
- iterator not dereferencable问题
STL中的迭代器总是出现各种问题,这个是我在打表达式求值时碰到的... 综合网上的答案,一般来说有两种情况: 第一:访问了非法位置. 一般来说可能在queue为空时取front(),rear(),或者 ...
- 使用路径arc-七彩
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...