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. var let const的一些区别

    var let const 都是来定义变量的. var let 作用域有些区别. const 类似于java中的常量的概念.即:只能给一个变量赋值一次,即指定一个引用. 举例来说: function ...

  2. ORA-00911: 无效字符 问题和解决

    1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...

  3. background-image 背景图片的设置

    background-image 背景图片的设置 属性:background-image: url(img/banner.jpg); 1.设置背景图的宽度 background-size: 400px ...

  4. Css Sprite(雪碧图、精灵图)<图像拼合技术>

    一.精灵图使用场景: 二.Css Sprite(优点) 减少图片的字节. 减少网页的http请求,从而大大的提高页面的性能. 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不 ...

  5. PHP基础3--文件加载-错误处理

    主要: 1-文件加载 2-错误处理 文件加载 文件加载语句 1) 4个文件加载语句:include,  require,   include_once,    require_once 2) 使用形式 ...

  6. 006---Linux用户、群组和权限

    用户.组.权限管理 用户管理 增 语法:useradd -c '添加用户1' user1 选项和参数: -c:用户的备注信息 -d:用户的家目录 -g:指定用户所属的用户组(gid) -s:指定用户登 ...

  7. java中stream部分笔记

    Stream流表面上看起来与集合类似,允许你转换和检索数据.然而,两者却有显著的不同1.流不存储元素.它们存储在底层的集合或者按需生成2.流操作不改变他们的源数据.例如filter方法不会从一个新流中 ...

  8. Java设计模式(20)——行为模式之命令模式(Command)

    一.概述 概念 类似C中的callback! UML简图 角色 客户端:创建具体命令,指定接收者 命令接口:声明命令的接口 具体命令:定义接收者和行为之间的弱耦合(execute执行方法) 请求者(I ...

  9. [Oracle]关于Oracle分页写法的性能分析及ROWNUM说明

    关于分页写法的性能分析及ROWNUM的补充说明 分页写法 一.测试前数据准备 SQL> SELECT COUNT(*) FROM BPM_PROCVAR; COUNT(*) ---------- ...

  10. 成都Uber优步司机奖励政策(3月9日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...