Reverse Words in a String  单词取反

    whowhoha@outlook.com

Question:

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

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

void reverseWords(string &s) {

vector <string>des;

if(s.empty())

return;

int len = s.size();

for(int i = 0; i < len; i++){

if(s[i] == ' ')

continue;

string word;

while(i < len && s[i] != ' '){

word += s[i];

i++;

}

des.push_back(word);

}

reverse(des.begin(), des.end());

if(des.empty())

s = "";

else {

s.clear();

int j ;

for(j = 0; j < des.size() -1; j++){

s += des[j];

s += ' ';

}

s += des[j];

}

}

leetcode6 Reverse Words in a String 单词取反的更多相关文章

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

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

  2. [Swift]LeetCode186. 翻转字符串中的单词 II $ 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 ...

  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 III 翻转字符串中的单词之三

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

  5. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  6. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

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

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

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

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

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

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

随机推荐

  1. HDOJ2018母牛的故事

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 一段C程序分析

    #include <stdio.h> #include <stdlib.h> void main() { int i; ; char ch; printf("请输入一 ...

  3. Android:Xml(读取与存储)

    1.读取XML文件 参数xml是建含xml数据的输入流,List<Person> persons用于存储xml流中的数据. XmlPullParser类的几个方法:next(),nextT ...

  4. C# WCF学习笔记(二)终结点地址与WCF寻址(Endpoint Address and WCF Addressing) WCF中的传输协议

    URI的全称是 Uniform Rosource Identifire(统一资源标识),它唯一标识一个确定的网绐资源,同时也表示资源所处的位置及访问的方式(资源访问所用的网络协议). 对于Endpoi ...

  5. (转)SqlServer2008 数据库同步的两种方式 (发布、订阅)

    上篇中说了通过SQL JOB的方式对数据库的同步,这一节作为上一节的延续介绍通过发布订阅的方式实现数据库之间的同步操作.发布订阅份为两个步骤:1.发布.2订阅.首先在数据源数据库服务器上对需要同步的数 ...

  6. JAVA解析XML的四种方式

    java解析xml文件四种方式 1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这 ...

  7. java web 简单的分页显示

    题外话:该分页显示是用 “表示层-控制层-DAO层-数据库”的设计思想实现的,有什么需要改进的地方大家提出来,共同学习进步. 思路:首先得在 DAO 对象中提供分页查询的方法,在控制层调用该方法查到指 ...

  8. mysql优化之定位问题

    首先先介绍几个关键字 1  show status 表示数据库当前的状态数据 show [session|global] status  session是当前连接的统计结果   global 是数据库 ...

  9. SMB/CIFS协议解析二

    一.拷贝文件(远程-->本地) 1.SMB_COM_NT_CREATE_ANDX (0xa2)       打开文件,获取文件名,获得读取文件的  总长度. 2.SMB_COM_READ     ...

  10. Mac OS X安装OpenCV 3.1.0

    在我的上一篇文章“”中已经介绍了Linux下OpenCV的安装配置方法,在这里仅仅记录Mac上相对于Linux的一点点差异. 1. 安装依赖包 Mac上安装软件包使用的工具是brew,用此来替代Ubu ...