1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/

反转字符串中的所有单词。

2. 思路:

这题主要是要注意空格的影响。比方说,string首尾和单词之间可能有一或多个空格。看到有人逐个对空格判断,但是我觉得逐个判断其实稍微容易出错(当然如果非常熟悉的话就完全无所谓啦),我的一个简单想法是用stringstream

PS:不熟悉stringstream的朋友可以看看链接的文档。

3. 代码

class Solution {
public:
string reverseWords(string s) {
stringstream ss;
ss << s;
string v, n;
while(ss >> n){
reverse(n.begin(),n.end());
v.append(n+' ');
}
v.pop_back();
return v;
}
};

  

Leetcode - 557. Reverse Words in a String III (C++) stringstream的更多相关文章

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

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

  2. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

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

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

  5. LeetCode 557 Reverse Words in a String III 解题报告

    题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...

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

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

  7. 557. Reverse Words in a String III【easy】

    557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...

  8. Week4 - 500.Keyboard Row & 557.Reverse Words in a String III

    500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...

  9. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

随机推荐

  1. Web项目开发中常见安全问题及防范

    计算机程序主要就是输入数据 经过处理之后 输出结果,安全问题由此产生,凡是有输入的地方都可能带来安全风险.根据输入的数据类型,Web应用主要有数值型.字符型.文件型. 要消除风险就要对输入的数据进行检 ...

  2. 系统优化怎么做-SQL优化

    大家好,这里是「聊聊系统优化 」,并在下列地址同步更新 博客园:http://www.cnblogs.com/changsong/ 知乎专栏:https://zhuanlan.zhihu.com/yo ...

  3. update更新修改数据

    update ---整表更新数据 update  表名  set  需要调整字段1= '值1' ,需要调整字段2= '值2'  …… ---更新条件数据 update  表名  set  需要调整字段 ...

  4. java中反射的基本使用

    fanShe.java package example5;class fanShe{    /*1.应用在一些通用性比较高的代码中.     *2.后面学的框架,大多数都是应用框架来实现的.     ...

  5. css中三种隐藏方式

    1.overflow 溢出隐藏 overflow:hidden 2.display 隐藏不占据原来的文档,即会让出空间 display:black  显示 display:none  隐藏 3.vis ...

  6. angularjs中控制器之间的通信----$on、$emit和$broadcast解析

    $on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...

  7. [Doctrine Migrations] 数据库迁移组件的深入解析三:自定义数据字段类型

    自定义type 根据官方文档,新建TinyIntType类,集成Type,并重写getName,getSqlDeclaration,convertToPHPValue,getBindingType等方 ...

  8. IA64与x86-64的区别

    win7 sp1下载地址:https://download.microsoft.com/download/0/A/F/0AFB5316-3062-494A-AB78-7FB0D4461357/wind ...

  9. JavaWeb——课程管理系统(2).java---18.11.29

    DaoFactory.java package com.jaovo.msg.Util; import com.jaovo.msg.dao.UserDaoImpl; public class DaoFa ...

  10. typescript入门,可以一起探讨提点意见互相学习。

    typescript是js的一个超集,TypeScript扩展了JavaScript的语法,所以任何现有的JavaScript程序可以不加改变的在TypeScript下工作.TypeScript是为大 ...