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. IOC-AutoFac

    学习过程中参考博客: AutoFac文档:http://www.cnblogs.com/wolegequ/archive/2012/06/09/2543487.html AutoFac使用方法总结:P ...

  2. iOS-WebView(WKWebView)进度条

    一直以来,就有想通过技术博客来记录总结下自己工作中碰到的问题的想法,这个想法拖了好久今天才开始着手写自己的第一篇技术博客,由于刚开始写,不免会出现不对的地方,希望各位看到的大牛多多指教.好了,不多说了 ...

  3. 学习 Linux_kernel_exploits 小记

    Linux_kernel_exploits+ 功能:自动生成UAF类型漏洞exp文件的工具,目前缺少文档介绍,可以参考test文件下的使用实例,但是源码中缺少dataflowanalyzer模块+ 相 ...

  4. 解决IDEA右键 new 没有新建class/Interface等等选项

    1.File->Project Structure 2.选择Modules-->右边Sources中选择所需目录 然后点击 Sources-->Apply-->OK 3.再在左 ...

  5. vue-cli3 vue.config.js 配置

    // cli_api配置地址 https://cli.vuejs.org/zh/config/ module.exports = { baseUrl: './', // 部署应用包时的基本 URL o ...

  6. Spring Security学习笔记(一)

    认证和权限控制 AuthenticationManager是认证的主要接口,它只有一个authenticate方法,可以做3件事情. 返回一个认证信息(Authentication),表示认证成功 抛 ...

  7. SublimeText配置Python3运行环境

    1.查看python3安装路径which python3 2.打开sublime text 3,点击上部菜单栏Tools->Build System->new Build System 3 ...

  8. python新手学习之文件读写之修改

    文件除r.w.a方式打开外,还可以有多种组合方式如r+ w+ a+等多种方式 1.r+ 读写模式介绍,开始读是从一行开始读,写永远从最后开始写(类似于追加) # f = open("test ...

  9. EOJ Monthly 2019.3 A

    A. 钝角三角形 单点时限: 3.0 sec 内存限制: 512 MB QQ 小方以前不会判断钝角三角形,现在他会了,所以他急切的想教会你. 如果三角形的三边长分别为 a, b, c (a≤b≤c), ...

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

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