Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?

151. Reverse Words in a String 一样,这里要求不能用额外空间,要用in-place完成。

该题假设开头和结尾没有空格,而且单词之间只有一个空格。不需要这些假设也是可以的,就是代码会比较复杂。
思路就是两步走,第一步就是将整个字符串翻转。然后从头逐步扫描,将每个遇到单词再翻转过来。

[注意事项]
1)如果是Java,应该跟面试官指出String是immutable,所以需要用char array来做。
2)follow-up问题:k-step reverse。也就是在第二部翻转的时候,把k个单词看作一个长单词,进行翻转。

Java:

public void reverseWords(char[] s) {
reverse(s, 0, s.length);
for (int i=0, j=0; j<=s.length; j++) {
if (j==s.length || s[j]==' ') {
reverse(s, i, j);
i = j + 1;
}
}
} private void reverse(char [] s, int begin, int end) {
for (int i=0; i<(end-begin)/2; i++) {
char temp = s[begin+i];
s[begin+i] = s[end-i-1];
s[end-i-1] = temp;
}
}

Python:

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] reverse(s, 0, len(s))
i = 0
for j in xrange(len(s) + 1):
if j == len(s) or s[j] == ' ':
reverse(s, i, j)
i = j + 1

C++:

class Solution {
public:
void reverseWords(string &s) {
int left = 0;
for (int i = 0; i <= s.size(); ++i) {
if (i == s.size() || s[i] == ' ') {
reverse(s, left, i - 1);
left = i + 1;
}
}
reverse(s, 0, s.size() - 1);
}
void reverse(string &s, int left, int right) {
while (left < right) {
char t = s[left];
s[left] = s[right];
s[right] = t;
++left; --right;
}
}
};  

类似题目:

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

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

All LeetCode Questions List 题目汇总

 

  

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

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

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

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

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

  6. [Swift]LeetCode186. 翻转字符串中的单词 II $ 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 ...

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

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

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

  9. Leetcode - 186 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-s ...

随机推荐

  1. Hive优化(整理版)

    1. 概述 1.1 hive的特征: 可以通过SQL轻松访问数据的工具,从而实现数据仓库任务,如提取/转换/加载(ETL),报告和数据分析: 它可以使已经存储的数据结构化: 可以直接访问存储在Apac ...

  2. zabbix4.2.5默认告警模板

    产生告警: Problem: {EVENT.NAME} Problem started at {EVENT.TIME} on {EVENT.DATE} Problem name: {EVENT.NAM ...

  3. 更新GitHub上自己 Fork 的代码与原作者的项目进度一致

    在GitHub上我们会去fork别人的一个项目,这就在自己的Github上生成了一个与原作者项目互不影响的副本,自己可以将自己Github上的这个项目再clone到本地进行修改,修改后再push,只有 ...

  4. RobotFrameWork框架介绍与安装

    一.RobotFrameWork介绍 1.简称RF,基于python研发的一款自动化测试框架.开源.跨平台的测试框架 2.RF是基于RF基金来研发的一款软件,目前已完全能够在python3环境下应用 ...

  5. Idea中,项目文件右键菜单没有svn选项处理办法

    问题描述 IntelliJ IDEA打开带SVN信息的项目,在项目文件上点击右键,菜单中没有Subversion的功能项,如下图: 解决办法 点击菜单:VCS -> Enabled Versio ...

  6. AtCoder Beginner Contest 132 解题报告

    前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using names ...

  7. 63、Spark Streaming:架构原理深度剖析

    一.架构原理深度剖析 StreamingContext初始化时,会创建一些内部的关键组件,DStreamGraph,ReceiverTracker,JobGenerator,JobScheduler, ...

  8. PHP base_convert() 函数

    16进制转8进制 <?php $hex = "E196"; echo base_convert($hex,,); ?> 8进制数转换为10进制数 <?php $o ...

  9. mysql abs() 获取绝对值

    mysql> -); +----------+ | abs(-) | +----------+ | | +----------+ row in set (0.00 sec)

  10. 【2019.11.18】SDN阅读作业

    为什么需要SDN?SDN特点? 随着网络的快速发展,传统互联网出现了如传统网络配置复杂度高等诸多问题,这些问题说明网络架构需要革新,可编程网络的相关研究为 SDN 的产生提供了可参考的理论依据 SDN ...