题目: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. springmvc文件上传2中方法

    基于前面文章的基础上. 一.准备 需要的jar  二.配置 1.  spmvc-servlet.xml <?xml version="1.0" encoding=" ...

  2. linux服务器下发送邮件

    系统管理人员经常会遇到对于设备或者任务的预警与通知,通常情况有发送短信.邮件等方式.发送短信一般来说需要有短信猫(硬件)或者调用libfetion给飞信用户发送.本文介绍几种简单的发送邮件的方式. 本 ...

  3. C#中SaveFileDialog 和OpenFileDialog 的用法

    1.OpenFileDialog private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new O ...

  4. PowerDesigner技巧

    原文:PowerDesigner技巧 1.PowerDesigner使用MySQL的auto_increment  ◇问题描述:  PD怎样能使主键id使用MySQL的auto_increment呢? ...

  5. Java文件解压之TGZ解压

    package com.alibaba.intl.batch.dependency; import java.io.File; import java.io.FileInputStream; impo ...

  6. Splunk常用命令

    重启/查看状态/停止splunk [root@localhost splunk]# /opt/splunk/bin/splunk restart / status / stop

  7. checkbox 全选、全不选、反选 插件

    jquery.checkbox.js: ;(function($,window,document,undefined){ $.fn.check=function(mode){ mode= mode | ...

  8. 公交wifi运营平台分析

    一.前言背景 昨晚下午,老板让看一些车载公交wifi后台管理的一些东西,这个随着移动端设备而兴起的wifi战,慢慢的也会越演越烈. 现在于很多人在外面的时候,进入一家店首先看的不是菜单,而是问一句“你 ...

  9. disable-linux-firewall-under-centos-rhel-fedora

    http://www.cyberciti.biz/faq/disable-linux-firewall-under-centos-rhel-fedora/

  10. 安卓学习之--如何关闭所有的activity

    根据Activity的声明周期 方法1   我们知道Android的窗口类提供了历史栈,我们可以通过stack的原理来巧妙的实现,这里我们在A窗口打开B窗口时在Intent中直接加入标志 Intent ...