1、题目描述

2、问题分析

使用一个vector存储每个单词。

3、代码

 void reverseWords(string &s) {
vector<string> v;
for (string::iterator it = s.begin(); it != s.end(); ) {
if (*it == ' ') {
it++;
}
else {
auto itr = it + ; while (*itr != ' ' && itr != s.end()) {
itr++;
} string sub = s.substr(it - s.begin(), itr - it);
v.push_back(sub);
if (itr == s.end())
break;
it = itr + ;
}
} s.clear();
for (vector<string>::reverse_iterator rv = v.rbegin(); rv != v.rend(); rv++) {
if (rv + != v.rend()) {
s += *rv;
s += ' ';
} else {
s += *rv;
}
} }

LeetCode 题解之Reverse Words in a String的更多相关文章

  1. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  2. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  3. 【LeetCode OJ】Reverse Words in a String

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

  4. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  5. 【LeetCode练习题】Reverse Words in a String

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

  6. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  7. 【刷题-LeetCode】151 Reverse Words in a String

    Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...

  8. 【一天一道LeetCode】#345. Reverse Vowels of a String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  9. LeetCode算法题-Reverse Words in a String III(Java实现)

    这是悦乐书的第259次更新,第272篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第126题(顺位题号是557).给定一个字符串,您需要反转句子中每个单词中的字符顺序,同 ...

随机推荐

  1. Android 开发服务类 05_ ApkPatchDemo

    APP 增量更新服务端[https://github.com/cundong/SmartAppUpdates] import com.cundong.common.Constants; import ...

  2. Android_EditText 打勾显示输入的密码 --EditText与setTransformationMethod

    实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...

  3. 对html中iframe的研究

    虽然平时不怎么用iframe,但经常在网上听一些前辈说iframe怎样怎样,今天索性对iframe来个大研究,那样就不必去记那些条条框框了,自己体验一遍比看什么都好. 创建两个文件一个index.ht ...

  4. spring boot 与 filter

    spring boot 里面用拦截器好像比用过滤器多一些. 在过滤器中, 并不能获取到action的相关信息, 会造成很多的麻烦和功能欠缺. 那, 这里就用过滤器做一个小栗子, 实际使用过程中, 不会 ...

  5. CentOS常用软件安装

    yum install *firefox* yum install flash-plugin

  6. 如何优雅的控制goroutine的数量

    1,为什么要控制goroutine的数量? goroutine固然好,但是数量太多了,往往会带来很多麻烦,比如耗尽系统资源导致程序崩溃,或者CPU使用率过高导致系统忙不过来.比如: ; i < ...

  7. CAS多点登录

    转自:http://www.blogjava.net/alwayscy/archive/2012/12/01/392322.html 场景 想要用到的场景:用户访问WEB服务,WEB访问非WEB服务1 ...

  8. c# LINQ用法

    一.什么是LINQ LINQ(读音link)代表语言集成查询(Language Integrated Query),是.NEt框架的扩展,它允许我们用SQL查询数据库的方式来查询数据的集合,使用它,你 ...

  9. Spring Boot项目属性配置

    接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将se ...

  10. [转]MSSQL中利用TOP提高IF EXISTS查询语句的性能

    本文转自:https://blog.csdn.net/f_r_e_e_x/article/details/51704784 --有可能返回一条或多个结果集,其实我们只需要知道是否 --有数据即可,这样 ...