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. SQL中对连表查询的建议

    多表连查时,如果存在多个唯一键可以做关联,尽可能选择有意义的code或name,能不选择无意义的id或者uuid最好! 所以在存储的时候也是这样,并且从始至终保持一致性.这样既降低了维护和阅读的难度, ...

  2. C#中小写人民币转大写

    /// <summary> /// 转换成大写人民币 /// </summary> /// <param name="myMoney">< ...

  3. LeetCode 中级 -二叉树的层次遍历(102)

    题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

  4. Jquery拼图

    Jquery代码 <script> $(function () { $("td").click(function () { var img = $(this).prop ...

  5. css:url链接去下划线+点击前黑色+点击时灰色+点击后黑色

    一般的文章列表 加了样式之后的效果 附上css代码 /*点击前*/ a:link{ color: black; } /*点击后*/ a:visited{ color: black; } /*点击时*/ ...

  6. HTML 5 audio标签

    audio标签的介绍 定义: <audio> 标签定义声音,比如音乐或其他音频流. <audio></audio>是HTML5中的新标签 能够在浏览器中播放音频, ...

  7. Waltz of love

    Waltz of love Love me tenderly Love me softly Close your eyes,fling to the dangcing hall Follow your ...

  8. 实验6 shell程序设计一(2)

    编写一段bash shell程序, 根据键盘输入的学生成绩,显示相应的成绩登等级, 其中 60分以下为"Failed!", 60-69分为"Passed!", ...

  9. 从Github开源项目《云阅》所学到的知识

    感谢开源,感谢大神,才让我们这些菜鸟成长! 附上云阅开源项目地址:点我吧. 1.轮播图的实现. 现在的APP基本都会实现这个功能吧,然后一直都找不到好的第三方库,能够满足各种需求.然而碰到了这个开源库 ...

  10. Fliptil_KEY

    Fliptil(fliptile.pas/c/cpp) [问题描述] 约翰知道,那些高智力又快乐的奶牛产奶量特别高.所以他做了一个翻瓦片的益智游戏来娱乐奶牛. 在一个M×N的骨架上,每一个格子里都有一 ...