reverse the string word by word
题目: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.最开始的空格怎么处理呢?我们用到
if (string_index < 0)break;
reverse the string word by word的更多相关文章
- 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 ...
- [Algorithm] Reverse array of Chars by word
For example we have: ["p", "r", "e", "f", "e", &qu ...
- Microsoft.Office.Interop.Word 创建word
Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- C#用Microsoft.Office.Interop.Word进行Word转PDF的问题
之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...
- word to word
Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...
- 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 ...
- 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 ...
- leetcode@ [79/140] Trie树应用 Word Search / Word Search II
https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...
随机推荐
- 【Linux高频命令专题(21)】df
概述 linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 显示指定磁盘文件的可用空间.如果没有文件名 ...
- .net与linux
Mono是什么? http://blog.sina.com.cn/s/blog_693ffe760100k5uh.html Mono是一个开放源代码,Linux版的Microsfot.NET开发平台 ...
- sizeof 和strlen的区别
1. 编译时计算运算符sizeof,可用类型或变量做参数,计算占用内存的大小.sizeof后若是类型必须加括弧,若是变量名可不加括弧.sizeof(x)可用来定义数组维数.如:printf(" ...
- ios开发--清理缓存
ios文章原文 一段清理缓存的代码如下: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) , ...
- Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 测试Tomcat
- 不重启使XP环境变量生效
不重启使XP环境变量生效 http://www.pkghost.cn/wz/sort0185/8874.html 在“我的电脑”->“属性”->“高级”->“环境变量”中增加或修改环 ...
- Android View 绘制过程
Android的View绘制是从根节点(Activity是DecorView)开始,他是一个自上而下的过程.View的绘制经历三个过程:Measure.Layout.Draw.基本流程如下图: per ...
- [51NOD1105]第k大的数(二分答案)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 先排序,二分上下界分别是最小的两个数和最大的两个数的乘积 ...
- yeoman开始项目
使用 yeoman 构建项目之前,你需要安装这两个环境:node,ruby. 为什么需要使用node?因为我们需要使用grunt自动化工具,而grunt工具则是依赖node. 为什么需要使用ruby? ...