题目: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. 什么叫非阻塞io

    而一个NIO的实现会有所不同,下面是一个简单的例子: ByteBuffer buffer = ByteBuffer.allocate(48); int bytesRead = inChannel.re ...

  2. WCF入门(六)---主机WCF服务

    建立一个WCF服务后,下一步就是托管它,以便客户端应用程序可以使用,这就是所谓的WCF服务托管. WCF服务可以通过使用任何的四种方法如下托管. IIS主机 - IIS是Internet信息服务的缩写 ...

  3. PL/SQL-Thread creation error:存储空间不足,无法处理此命令

    PL/SQL中执行SQL语句,提示“Thread creation error:存储空间不足,无法处理此命令”.查找了解决方案,如下: 1. 单击开始,然后单击运行. 2. 键入 regedit,然后 ...

  4. Telerik RadGrid Demo

    List Page Html code: <%@ Page Language="vb" AutoEventWireup="false" CodeBehin ...

  5. IIS下PHP的三种配置方式比较

    在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...

  6. Oracle学习之集合运算

    一.集合运算操作符  UNION:(并集)返回两个集合去掉重复值的所有的记录  UNION ALL:(并集)返回两个集合去掉重复值的所有的记录 INTERSECT:(交集)返回两个集合的所有记录,重复 ...

  7. Fedora20 优化体验

    玩了些许天的fedora系统,到底是加深了对于linux系统的了解 为了便于大家对于fedora系统支持,我将这些天对于fedora的一些不适之处及改进的策略进行了一下小总结.便于新手对于fedora ...

  8. 函数ut_bit_set_nth

    /*****************************************************************//** Sets the nth bit of a ulint. ...

  9. HighCharts 饼图

    @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section ...

  10. hdu 4609 3-idiots(快速傅里叶FFT)

    比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...