Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

此题要注意的几个情况是:

(1)输入字符串可能含有前缀0和后缀0

(2)字符串中每个单词之间可能含有多个空格

(3)字符串是空串

(3)字符串只有一个单词

此题主要是根据空格分隔单词,然后将分隔后单词的顺序逆转一下即可

  1. void reverseWords(string &s){
  2. string buf;
  3. stringstream ss(s);
  4. vector<string> word;
  5. while(ss >> buf) word.push_back(buf);
  6. if(word.size() == ) s="";
  7. else{
  8. s="";
  9. int n = word.size();
  10. for(int i = n -; i >=; -- i){
  11. if(i!=n-) s+=" ";
  12. s+=word[i];
  13. }
  14. }
  15. }
 

Leetcode Reverse Words in a String的更多相关文章

  1. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  2. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  3. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  4. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  5. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  6. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  7. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  8. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  9. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  10. [leetcode]Reverse Words in a String @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...

随机推荐

  1. Android touch事件的派发流程

    Android TouchEvent事件传递机制 通俗易懂,能够了解Touch事件派发的基本流程. Android中的dispatchTouchEvent().onInterceptTouchEven ...

  2. Newtonsoft.Json(Json.Net)学习笔记(转)

    概述 Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库,通过Nuget获取.(查看原文) 下面是Json序列化和反序列化的简单封装: /// <summary&g ...

  3. HTML CSS微信CSS显示一些总结

    微信显示网页是调用腾讯自带的浏览器内核,由于腾讯浏览器内核对css展示效果没有谷歌浏览器好,导致用谷歌浏览器写好的网页,放到微信页面之后,显示的效果就发生变化,所以调整css样式显得那么吃力: 1. ...

  4. apache linux 安装

    sudo apt-get install zlib1g-dev 1.到官网下载,然后解压httpd-2.4.18.tar.gz 2.下载apr-1.5.2.tar.gz并解压    http://ar ...

  5. VS2015 Xamarin for iOS

    VS2015环境配置 VS2015安装不多说.其实Xamarin 和微软感觉并不是什么好基友,Xamarin以前一直像个可怜的娃,以插件的形式寄生于VS中.现在只不过形势稍微好点了,VS2015 在明 ...

  6. Oracle Segments可以跨多个data files吗?

    首先,你需要明白的一点是:数据库的物理结构是由数据库的操作系统文件所决定,每一个Oracle数据库是由三种类型的文件组成:数据文件.日志文件和控制文件.数据库的文件为数据库信息提供真正的物理存储.每一 ...

  7. phpcms v9 常用调用标签(全)

    本文介绍phpcms v9中模板标签使用说明. {template ) {==}   {/,,)}     loop是data的时候用{thumb($v[thumb],,)} 分页标签------{$ ...

  8. (四)WebRTC手记之本地音频采集

    转自:http://www.cnblogs.com/fangkm/p/4374668.html 上一篇博文介绍了本地视频采集,这一篇就介绍下音频采集流程,也是先介绍WebRTC原生的音频采集,再介绍C ...

  9. Servlet中的GET和POST之间的区别

    自己的感悟: get和post这是http协议的两种方法,另外还有head, delete等 这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串.post的 ...

  10. Java学习随笔2:Java复合赋值表达式的小问题

    问题:i += j只是i = i + j的简写么? 答案:非也!看下面的程序: int i = 5; long j = 8; i += j; // 可以通过编译且结果正确 i = i + j; // ...