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

#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. NeHe OpenGL lession 4

    // lession4.c #include <OpenGL/OpenGL.h> #include <GLUT/GLUT.h> #include <stdio.h> ...

  2. Oracle 表三种连接方式(sql优化)

    在查看sql执行计划时,我们会发现表的连接方式有多种,本文对表的连接方式进行介绍以便更好看懂执行计划和理解sql执行原理. 一.连接方式: 嵌套循环(Nested Loops (NL)) (散列)哈希 ...

  3. IOS中的自动布局

    Autolayout是一种“自动布局”技术,专门用来布局UI界面 Autolayout能很轻松地解决屏幕适配问题 Autolayout的两条核心概念:   >1 参照:通过参照其他控件或父控件来 ...

  4. JavaScript 高级程序设计(第3版)笔记——chapter6:面向对象的程序设计

    一.创建对象 工厂模式.使用简单的函数创建对象,为对象添加属性和方法,然后返回对象.[问题:没有解决对象识别问题] 1 function createPerson(name, age) { 2 var ...

  5. ACE6.2.0文件/目录操作

    文件读取.#include "ace/FILE_Connector.h"#include "ace/FILE_IO.h"void fileRW(){ACE_FI ...

  6. 新建linux组、用户命令

    之前在安装oracle的时候,出现了一个问题:安装Oracle,新建组.用户的时候的一个错误.看这篇博客前,先看这个链接,学习要从解决出现的问题出手. 建立用户组和用户 下面总结一下Linux建立组和 ...

  7. foreach学习笔记

    对集合进行遍历 只能获取集合元素,但是不能对集合进行操作. 迭代器除了遍历,还可以进行remove的动作. 如果是用ListIterator,还可以在遍历过程中进行增删改查的动作. for(Strin ...

  8. PHP 字符串处理 总结

    PHP 字符串处理 PHP 字符串处理 PHP 的字符串处理功能非常强大,主要包括: 字符串输出 echo():输出一个或多个字符串 print():输出一个字符串 printf():输出格式化字符串 ...

  9. Android Bug 记录

    1.Unable to resolve target 'android-5' 无法解析目标 ' 安卓系统-5'      Unable to resolve target 'Google Inc.:G ...

  10. window下nodejs安装指南

    相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护 ...