Given an input string , reverse the string word by word.

Example:

Input:  ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]

Note:

  • A word is defined as a sequence of non-space characters.
  • The input string does not contain leading or trailing spaces.
  • The words are always separated by a single space.

Follow up: Could you do it in-place without allocating extra space?

这道题让我们翻转一个字符串中的单词,跟之前那题 Reverse Words in a String 没有区别,由于之前那道题就是用 in-place 的方法做的,而这道题反而更简化了题目,因为不考虑首尾空格了和单词之间的多空格了,方法还是很简单,先把每个单词翻转一遍,再把整个字符串翻转一遍,或者也可以调换个顺序,先翻转整个字符串,再翻转每个单词,参见代码如下:

解法一:

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

我们也可以使用 C++ STL 中自带的 reverse 函数来做,先把整个字符串翻转一下,然后再来扫描每个字符,用两个指针,一个指向开头,另一个开始遍历,遇到空格停止,这样两个指针之间就确定了一个单词的范围,直接调用 reverse 函数翻转,然后移动头指针到下一个位置,在用另一个指针继续扫描,重复上述步骤即可,参见代码如下:

解法二:

class Solution {
public:
void reverseWords(vector<char>& str) {
reverse(str.begin(), str.end());
for (int i = , j = ; i < str.size(); i = j + ) {
for (j = i; j < str.size(); ++j) {
if (str[j] == ' ') break;
}
reverse(str.begin() + i, str.begin() + j);
}
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/186

类似题目:

Reverse Words in a String III

Reverse Words in a String

Rotate Array

参考资料:

https://leetcode.com/problems/reverse-words-in-a-string-ii/

https://leetcode.com/problems/reverse-words-in-a-string-ii/discuss/53851/Six-lines-solution-in-C%2B%2B

https://leetcode.com/problems/reverse-words-in-a-string-ii/discuss/53775/My-Java-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  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 III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

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

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

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

  5. 186. Reverse Words in a String II 翻转有空格的单词串 里面不变

    [抄题]: Given an input string , reverse the string word by word. Example: Input: ["t"," ...

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

  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] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

随机推荐

  1. 跨域之Ajax

    提到Ajax,一般都会想到XMLHttpRequest对象,通过这个对象向服务器发送请求,可以实现页面无刷新而更新数据. 由于同源策略的限制,一般情况下,只能通过XMLHttpRequest对象向同源 ...

  2. 深入理解CSS动画animation

    × 目录 [1]定义 [2]关键帧 [3]动画属性 [4]多值 [5]API 前面的话 transition过渡是通过初始和结束两个状态之间的平滑过渡实现简单动画的:而animation则是通过关键帧 ...

  3. [C1] 分离 C1FlexGrid 滚动条

    一  场景介绍 Silverlight 5.0 的 C1FlexGrid 控件里自带的滚动条,是嵌入在 C1FlexGrid 宽度和高度的范围里的,效果如下图所示: (未隐藏自带滚动条) (隐藏自带的 ...

  4. 如何在虚拟机里安装Linux

    本篇仅为作业... 实验课程:Linux 实验机器:联想y410p 指导老师:刘臣奇 实验时间:2016年9月25日 学生学号:140815 姓名:杨文乾 一.先安装虚拟机,之后创建一个新的虚拟机 之 ...

  5. jquery遍历选中checkbox的id

    $("[name='chkAll']:[checked]").each(function () { alert($(this).attr("id")); })

  6. Yii2.x 互斥锁Mutex-类图

  7. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  8. Android Weekly Notes Issue #232

    Android Weekly Issue #232 November 20th, 2016 Android Weekly Issue #232 本期内容包括: Kotlin的优势讨论; MVVM模式结 ...

  9. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  10. UINavigationBar 和view 重叠覆盖问题

    如果没有是storyboard进行界面设计,在ios7之后会遇到rootviewcontroller的view被navigationbar遮盖的问题,其实很好解决 - (void)viewDidLoa ...