题目:Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

要求:

1)首尾有空格的时候,反转后的string要将空格去掉

2)当string有多个连续空格的时候,只保留一个空格、

代码分析:

对多余空格剔除:

思路分析:

1)从原始s 的最末尾开始扫描,如果遇到空格,用while剔除掉。

2)接下来从第一个非空格读取,存入一个temp的string中,然后调用string::reverse() 反转,并string::append()到ss中。

3)重复1、2步,但是有几个情况需要注意:

a.我们从末尾扫描,当扫描到空格,后,由于ss为空,所以不会push.back(' ')到ss中。

b.中间有空格的,我们剔除完空格后就需要再增加一个' ',

c.最开始的空格怎么处理呢?我们用到

  1.  if (string_index < 0)     
  2.        break;
也就是剔除了空格后,如果此时的index已经是最开头了,那么我们就退出循环。
 
 
 
 
 

reverse the string word by word的更多相关文章

  1. LeetCode 5:Given an input string, reverse the string word by word.

    problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...

  2. [Algorithm] Reverse array of Chars by word

    For example we have: ["p", "r", "e", "f", "e", &qu ...

  3. Microsoft.Office.Interop.Word 创建word

    Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...

  4. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  5. C#用Microsoft.Office.Interop.Word进行Word转PDF的问题

    之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...

  6. word to word

    Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...

  7. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  8. leetcode@ [139/140] Word Break & Word Break II

    https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine ...

  9. leetcode@ [79/140] Trie树应用 Word Search / Word Search II

    https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...

随机推荐

  1. Apache编译与安装 RedHat enterprises 6.2

    引自:http://blog.chinaunix.net/uid-26881541-id-3336614.html http://apr.apache.org/download.cgi 命令: yum ...

  2. Eclipse groovy in action

    Eclipse :Version: Juno Service Release 2GrEclipse plugins:http://dist.springsource.org/release/GRECL ...

  3. 1709. Penguin-Avia(并查集)

    1709 简单题 并查集找下就行 #include <iostream> #include<cstdio> #include<cstring> #include&l ...

  4. Qt 多线程学习

    最近的项目上用到了关于多线程的知识,自己也比较感兴趣,所以就拿了那本<C++ GUI Qt4 编程>来学习. 这本书的第14章是关于多线程的知识,使用的Qt版本是Qt4.x.在下用的是最新 ...

  5. poj 1986 Distance Queries(LCA:倍增/离线)

    计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...

  6. ZigZag Conversion1

    问题描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  7. Android PRODUCT_COPY_FILES 自动拷贝文件

    /********************************************************************** * Android PRODUCT_COPY_FILES ...

  8. web页面性能测试

    做Web开发,难免要对自己开发的页面进行性能检测,自己写工具检测,工作量太大.网上有几款比较成熟的检测工具,以下就介绍一下,与大家分享. 互联网现有工具 基于网页分析工具: 1.       阿里测 ...

  9. HelloX操作系统与中国移动OneNET物联网平台成功完成对接

    HelloX成功与中国移动物联网平台对接 经过HelloX项目组同仁的努力,尤其是Tywin(@飓风)的努力下,HelloX最新版本V1.78已成功与中国移动OneNET(open.iot.10086 ...

  10. ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

    这里请注意,在编译ffmpeg时,不要使用--disable-devices选项. 使用 --enable-encoder=rawvideo --enable-decoder=rawvideo 启用r ...