题目: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. C 语言学习guideline

    Kernighan和Ritchie的<The C Programming Language>(中译名<C程序设计语言>)堪称经典中的经典,不过旧版的很多内容都已过时,和现在的标 ...

  2. iOS UICollectionView简单使用

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

  3. Linux 查看版本详情

    内核版本的信 uname -a -a选项表示察看所有的信息,但是从输出信息可以看出来,uname看到的版本信息,只是内核版本的信息,而不是发行版的版本信息 查看发行版信息 $cat /etc/issu ...

  4. HTML CSS3 手风琴菜单

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...

  5. 简单的XPath入门

    XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言.XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.XPath 是 XML 的查询语 ...

  6. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  7. [58 Argo]让argo跑起来

    接上一章,使用命令mvn jetty:run启动Argo,进入localhost的页面: 58在这里给了几种常见的访问和传值方法的示例,当点击到第三条<区分queryString和form参数& ...

  8. UVa 10561 (SG函数 递推) Treblecross

    如果已经有三个相邻的X,则先手已经输了. 如果有两个相邻的X或者两个X相隔一个.,那么先手一定胜. 除去上面两种情况,每个X周围两个格子不能再放X了,因为放完之后,对手下一轮再放一个就输了. 最后当“ ...

  9. FTP出现211-Extension supported 停止的解决方法

    FTP出问题211-Extension supported 停止的解决方法 FTP出问题211-Extension supported 停止的解决方法 FLASHFXP FTP上传登录时提示Exten ...

  10. codeforces 334A - Candy Bags

    忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...