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.最开始的空格怎么处理呢?我们用到

  1.  if (string_index < 0)     
  2.        break;

也就是剔除了空格后,如果此时的index已经是最开头了,那么我们就退出循环。

来自为知笔记(Wiz)

reverse the string word by word

时间: 2024-10-11 17:27:23

reverse the string word by word的相关文章

Word Break &amp;&amp; Word Break II

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 space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example,

Word Break/Word Segment

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

Microsoft.Office.Interop.Word 创建word

Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746.html 功能总结或者完善. 一.添加页眉 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System

如何将latex转换为word之word插入.bib中的文献

论文一直用latex写的,投稿需要,现在要将latex的论文转换到word中.难点之一:插入以前bib中的文献. 工具准备: 1 MiKTeX核心,如果已经装个Tex软件,比如CTeX就不用装了. 2 JabRef,BibTeX形式的参考文献管理工具. 3 Bibtex4Word软件.解压后将文件夹中的"bibtex4word.dot"文件,复制粘贴至Word的启动文件夹.我的是Win8系统,所在路径是:C:\Users\Administrator\AppData\Roaming\Mi

PDF转Word、Word转PDF技巧

随着PDF文件的流行,与之相关的PDF转Word.Word转PDF的操作在日常办公中也是比较常见了,PDF转Word的方法也比较多,今天就给大家分享一种简单的转换文档格式的方法. 1.先打开手机,在手机应用市场上搜索"PDF转换大师",找到这个软件后,将其下载到安卓手机上. 2.然后打开软件,在软件上面有两个不同的转换功能,如果是将PDF转Word,点击上面的"PDF转其他格式",选择这项功能. 3.接着再找到想要转换的PDF文件,可在"全部文件"

[email&#160;protected] [139/140] Word Break &amp; Word Break II

https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet"

aspose.word 读取word段落内容

aspose.word 插件下载 链接: http://pan.baidu.com/s/1qXIgOXY 密码: wsj2 使用原因:无需安装office,无兼容性问题,破解版有一定限制 代码: public ParagraphCollection WordParagraphs(string fileName) { Document doc = new Document(fileName); if (doc.FirstSection.Body.Paragraphs.Count > 0) { re

word to word

Question: For each word, you can get a list of neighbor words by calling getWords(String), find all the paths from word1 to word2. 1 public class Solution { 2 List<List<String>> finalList = new ArrayList<>(); 3 4 public static void main(