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.

151. Reverse Words in a String186. Reverse Words in a String II 的类似题目,这题是让翻转字符串里的单词。

Python:One Place

class Solution(object):
def reverseWords(self, s): def reverse(s, begin, end):
for i in xrange((end - begin) // 2):
s[begin + i], s[end - 1 - i] = s[end - 1 - i], s[begin + i] s, i = list(s), 0
for j in xrange(len(s) + 1):
if j == len(s) or s[j] == ' ':
reverse(s, i, j)
i = j + 1
return "".join(s)

Python:New Array

class Solution2(object):
def reverseWords(self, s):
reversed_words = [word[::-1] for word in s.split(' ')]
return ' '.join(reversed_words)

C++:

class Solution {
public:
string reverseWords(string s) {
for (int i = 0, j = 0; j <= s.length(); ++j) {
if (j == s.length() || s[j] == ' ') {
reverse(s.begin() + i, s.begin() + j);
i = j + 1;
}
}
return s;
}
};

     

类似题目:

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

[LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

All LeetCode Questions List 题目汇总

[LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III的更多相关文章

  1. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

  3. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

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

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

  6. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

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

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

  8. C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  9. Java实现 LeetCode 557 反转字符串中的单词 III(StringBuilder的翻转和分割)

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

随机推荐

  1. python基础---python基础语法

    1.常用符号 逗号,枚举:一个函数有多个参数sum(1,2) 等于,赋值:把一个值,给一个变量,a=1 括号,函数的参数部分sum(x,y) 冒号,一个子过程的开始 双引号/单引号:表示字符串 运算符 ...

  2. O(n) 取得数组中每个元素右边最后一个比它大的元素

    题目 2019.9.7,icpc徐州网络赛的E题 XKC's basketball team ,计蒜客上还可以做. 链接:https://nanti.jisuanke.com/t/41387 Inpu ...

  3. APT 信息收集——shodan.io ,fofa.so、 MX 及 邮件。mx记录查询。censys.io查询子域名。

    信息收集 目标是某特殊机构,外网结构简单,防护严密.经探测发现其多个子机构由一家网站建设公司建设. 对子域名进行挖掘,确定目标ip分布范围及主要出口ip. 很多网站主站的访问量会比较大.往往主站都是挂 ...

  4. appium+python自动化64-使用Uiautomator2执行driver.keyevent()方法报错解决

    前言 未加'automationName': 'Uiautomator2'参数使用Uiautomator可以正常使用driver.keyevent()方法,使用Uiautomator2时driver. ...

  5. c++查询特定字符串位置

    size_t find (const string& str, size_t pos = 0) const noexcept;(摘自c++官网:std::string::find) size_ ...

  6. MySQL的增、删、改、查

    数据库的常用命令以及作用 用法 作用 CREATE database 数据库名称. 创建新的数据库 DESCRIBE 表单名称; 描述表单 UPDATE 表单名称 SET attribute=新值 W ...

  7. C#锐利体验2读书笔记

    匿名方法,迭代,匿名方法允许我们以一种“内联”的方法来编写方法代码;匿名方法是直接与委托实例化相关联的,使委托实例化更加直观方便.匿名方法的几个相关问题--参数列表,--返回值,--外部变量. add ...

  8. POJ - 3728:The merchant (Tarjan 带权并查集)

    题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路 ...

  9. LeetCode(数据库)部门最高工资

    ), Salary int, DepartmentId int) )) Truncate table Employee ') ') ') ') Truncate table Department ', ...

  10. linux 读取文件

    linux读取文件是经常要用到的操作,以下示例(说明看注释): #读取文件snlist.txt中的每一行内容赋给sn变量 while read sn do echo ">>> ...