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?

思路:这题比另一个reverse words要简单,因为不存在多余空格的问题。

因此in-place方法很简单,先把整个字符串反转,然后依次把字符串中的每个单词再反转一次。

 class Solution {
public:
void reverseWords(string &s) {
int l = , r = s.size() - ;
while (l < r)
swap(s[l++], s[r--]);
for (int i = , n = s.size(); i < n;)
{
for (r = i + ; r < n && s[r] != ' '; r++);
l = i, r--;
i = r + ;
while (l < r)
swap(s[l++], s[r--]);
}
}
};

Reverse Words in a String II -- LeetCode的更多相关文章

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

  2. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

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

  4. Reverse Words in a String I & Reverse Words in a String II

    Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...

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

  6. leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

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

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

  8. 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...

  9. 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. jvm可视化工具jvisualvm插件——Visual GC

    转自:http://blog.csdn.net/xuelinmei_happy/article/details/51090115 Visual GC是一个Java 内存使用分析与GC收集的可视化工具插 ...

  2. WordCount 基础功能

    软测第一次作业 该项目在码云上的地址: https://gitee.com/zhege/WordCount 一,概述 WordCount的基础功能需求分析大致如下:对程序设计语言源文件统计字符数.单词 ...

  3. Canvas 剪切图片

    /** * 剪切图像 */ function initDemo8(){ var canvas = document.getElementById("demo8"); if (!ca ...

  4. day04_08-while查询所有行

    <?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...

  5. 【转载】Unity3D研究院transform.parent = parent坐标就乱了

    昨天有朋友问我了一个问题,它将Hierarchy视图里的某个子节点下的GameObject拷贝到另外一个对象的子节点下面,他使用的方法就是 transform.parent = parent 但是拷贝 ...

  6. rest_framework_jwt

    安装配置 安装 pip install djangorestframework-jwt 配置 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ...

  7. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  8. 机器学习框架Tensorflow数字识别MNIST

    SoftMax回归  http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 我们的训练集由  个已标记的样本构成: ,其 ...

  9. 巧用Fiddler代理来禁止资源缓存,从而达到每次都是从服务器加载最新的资源

    Fiddler ->  Rules ->  Performance  -> Disable Caching 直接设置禁用缓存,再在没有清除缓存功能的APP 中重新加载最新的页面, 每 ...

  10. foreach的理解

    foreach的两种写法的解读 一.常见 1.理解:将数组元素逐个赋值给变量V,然后将v输出 2.代码: $arr = array(1,2,3,4,5); foreach($arr as $a){ e ...