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 ...
随机推荐
- ios开发图片点击放大
图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现 创建了一个专门用于放大图片的类,以下为.h文件 #import <Foundation/Foundation. ...
- TCL语言笔记:TCL基础语法
一.什么是TCL Tcl 全称是 Tool command Language.它是一个基于字符串的命令语言,基础结构和语法非常简单,易于学习和掌握. Tcl 语言是一个解释性语言,所谓解释性是指不象其 ...
- python list去重的方法
转载于:http://yxmhero1989.blog.163.com/blog/static/112157956201381443244790/ Python很简洁 我们喜欢简单有效的代码 一. ...
- IDEA查找功能小结
查找类:Ctrl + N 支持模糊查询
- C++:向函数传递对象(对象、对象指针、对象引用)
3.5.1 使用对象作为函数参数,其方法与传递基本类型的变量相同 //例3.21 使用对象作为函数参数 #include<iostream> using namespace std; ...
- shell编程基础(3)条件判断语句
1,带参数的shellscript #this is program build 5.11 to test shell script ############ cxz ####### 5.11 ### ...
- 实例学习Bloom Filter
0. 科普1. 为什么需要Bloom Filter2. 基本原理3. 如何设计Bloom Filter4. 实例操作5. 扩展 0. 科普 Bloom Filter是由Bloom在1970年提出的一种 ...
- hadoop拾遗(三)---- 多种输入
虽然一个MapReduce作业的输入可能包含多个输入文件(由文件glob.过滤器和路径组成),但所有文件都由同一个InputFormat和同一个Mapper来解释.然而,数据格式往往会随时间而演变,所 ...
- Support Library官方教程(3)android studio中导入支援包
Support Library Setup How you setup the Android Support Libraries in your development project depend ...
- BZOJ3122 随机数生成器
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3122 题意: 思路:(1)x1=t则n=1; (2)a=0,则b=t,n=2;否则无解: ...