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. LeetCode LCP 3 机器人大冒险

    题目解析: 对于本题主要的核心是对于一个指令字符串如“RURUU”,如果我们假设它的终点坐标为(8,8),其实只要统计指令字符串中的R的个数和U的个数(对于我给出的例子而言,num_R == 2,nu ...

  2. wordpress调用缩略图/特色图url

    调用缩略图的url <a href="<?php the_post_thumbnail_url( 'full' ); ?>"><?php the_po ...

  3. CORS 跨域 node |XMLHttpRequest 跨域提交数据 node

    node服务端 app.post('/getdata',function(req,res,next){ req.setEncoding('utf8'); res.setHeader('Access-C ...

  4. 9、Hadoop配置文件和HDFS垃圾回收

    配置文件 默认配置文件:相对应的jar包中 core-default.xml hdfs-default.xml yarn-default.xml mapred-default.xml 自定义配置文件 ...

  5. 异常过滤器的好坏(CLR)

    为什么有些语言支持它们而另一些不支持呢?把它们加到我的新语言里是个好主意吗?我应该什么时候使用过滤器和catch/rethrow?就像很多事情一样,异常过滤器有好的一面也有坏的一面… 什么是异常过滤器 ...

  6. HTML5 离线应用

    一.离线应用cache manifes文件 HTML5中构建了一个离线(无网络状态)应用,只需创建一个cache manifest文件 可以配置需要的缓存的资源,网络无连接应用任然可以使用,本地读取缓 ...

  7. 洛谷 P2136 拉近距离 题解

    P2136 拉近距离 题目背景 我是源点,你是终点.我们之间有负权环. --小明 题目描述 在小明和小红的生活中,有N个关键的节点.有M个事件,记为一个三元组(Si,Ti,Wi),表示从节点Si有一个 ...

  8. MAC 隐藏功能

    finder 类: shift+ cmd + G  (去指定路径) cmd+↑ (返回) cmd+↓(打开当前选中的文件,如果没有选中的则去选中第一个) cmd+ o (打开当前选中的文件) 以下这些 ...

  9. rust 函数的使用

    fn main() { println!("Hello, world!"); another_function(2,3); let y ={ let x =3; //表达式的结尾没 ...

  10. Pygame 贪吃蛇

    目录 代码 遇到的问题 参考 代码 #-*-encoding=utf-8-*- # Wormy(a Nibbles clone) # By Al Sweigart al@inventwithpytho ...