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)字符串只有一个单词

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

void reverseWords(string &s){
string buf;
stringstream ss(s);
vector<string> word;
while(ss >> buf) word.push_back(buf);
if(word.size() == ) s="";
else{
s="";
int n = word.size();
for(int i = n -; i >=; -- i){
if(i!=n-) s+=" ";
s+=word[i];
}
}
}
 

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. Python中format的用法

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和: ...

  2. python中读取\写入CSV中数据的方法

  3. php上传文件进度条

    ps:本文转自脚本之家 Web应用中常需要提供文件上传的功能.典型的场景包括用户头像上传.相册图片上传等.当需要上传的文件比较大的时候,提供一个显示上传进度的进度条就很有必要了. 在PHP 5.4以前 ...

  4. php 分页查询

    get方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  5. Vs注释,vsXML,VSXML注释

    标签名称 说明 语法 参数 <summary> <summary> 标记应当用于描述类型或类型成员.使用<remarks> 添加针对某个类型说明的补充信息. < ...

  6. 重温WCF之消息拦截与篡改(八)

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...

  7. [Nodejs]十分钟快速编写简单静态文件服务器

    学了几天Nodejs 后我又干上了前端的活.这次遇到的问题是,我想在不同的设备上方便的查看我编写的网页,很自然的就想到要是能在本地搭建一个简单的http服务器的话,那局域网内的所有设备都可以访问了,这 ...

  8. dblink

    drop database link "STANDARD"; drop database link "CSPS"   --创建dblink create dat ...

  9. 使用PHP获取时间今天 明天 昨天 时间戳的详解

    使用php获取时间今天明天昨天时间戳 2013-06-20 11:12 <?php echo "今天:".date("Y-m-d")."< ...

  10. windows多线程详解

    转自:http://blog.csdn.net/zhouxuguang236/article/details/7775232 在一个牛人的博客上看到了这篇文章,所以就转过来了,地址是http://bl ...