我的思路:先读取每一个单词,存放到容器中;读取完毕后,将容器中的单词倒序写入输出中。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void f(string &s){
vector<string> vs;
string temp="";
int i=;
while(s[i]!='\0'){ //遍历直到结尾
if (s[i]==' '){ //遇到空格
if (!temp.empty())
vs.push_back(temp); //单词放入容器中
++i; // 跳过空格
temp=""; }
else{
temp.push_back(s[i]); //将字母加到temp字符串后面
++i;
}
}
if (!temp.empty())
vs.push_back(temp); //将最后一个单词放入容器中 int len=vs.size(); //倒序遍历容器
string res="";
for(int j=;j<len;++j){
res.append(vs[len-j-]);
if(j!=len-)
res.push_back(' ');
}
s=res;
} int main(){
string s;
getline(cin,s);
f(s);
cout<<s<<endl;
}

以上只是一种实现方法,还应该有更加高效的方法,欢迎讨论。

Reverse Words in a String | LeetCode OJ | C++的更多相关文章

  1. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  2. Reverse Words in a String——LeetCode

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

  3. Reverse Words in a String leetcode

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

  4. Reverse Words in a String leetcode java

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

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

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

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

随机推荐

  1. Scraping JavaScript webpages with webkit | WebScraping.com

    Scraping JavaScript webpages with webkit | WebScraping.com Scraping JavaScript webpages with webkit ...

  2. java 解析国密SM2算法证书

    首先说明用Java自带的解析x509证书类,是不能解析sm2算法的证书,执行会抛出异常. 用开源库bouncycastle能够解析.详细代码 private byte[] getCSPK(byte[] ...

  3. adb shell top

    PID:进程在系统中的ID CPU% - 当前瞬时所以使用CPU占用率 #THR - 程序当前所用的线程数 UID - 运行当前进程的用户id Name - 程序名称android.process.m ...

  4. 多条件搜索拼接Sql语句

    1. 如下实例:     1.1 如下图所示:[通过用户输入的数据拼接Sql搜索语句]                  1.2         private void button2_Click( ...

  5. JavaSE学习总结第24天_多线程2

      24.01  JDK5之后的Lock锁的概述和使用 虽然我们可以理解同步代码块和同步方法的锁对象问题,但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁,为了更清晰的表达如何加锁和释放锁,JDK ...

  6. Oracle/Mysql批量插入的sql,效率比较高

    1.oracle 批量插入: insert into tableName(col1,col2,col3...)    select 1,'第一行第一列值','第二列值' from dual union ...

  7. poj 1838

    http://poj.org/problem?id=1838 并查集,,,计算总共个数的模版..... #include <iostream> #define maxn 16006 #in ...

  8. 0课程介绍(Week1,3月3日)

    一.自我介绍 1.姓名:杨晔 2.办公室:B211-2 3.电子邮件:yangye@zjjy.com.cn 4.QQ:6706892 5.博客:http://www.cnblogs.com/meety ...

  9. ASP.NET JQuery 随笔-搜索框默认提示

    一.文本框中创建默认文本提示 通常用户在搜索内容时,在文本框输入内容前,文本框都会给出默认提示,提示用户输入正确的内容进行搜索. 当文本框获得焦点,如果文本框内容跟提示内容一样,提示内容会自然消失. ...

  10. SQLite取值时的下标与创建表中字段的关系