描述

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"

Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

分析

用istringstream来读取string中的每一个word,然后对每一个word通过reverse函数反转。

代码如下:

class Solution {
public:
string reverseWords(string s) {
string word; //hold every word in the string
string ret;
istringstream ss(s);
while(ss>>word){ //read a word
reverse(word.begin(),word.end()); //reverse it
ret = ret + word + " "; //splice them
}
return ret.substr(0,ret.size() - 1); //remove ' ' at end of the string
}
};

leetcode解题报告(25):Reverse Words in a String III的更多相关文章

  1. [LeetCode&Python] Problem 557. Reverse Words in a String III

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  2. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  3. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

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

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

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

  8. 53. leetcode557. Reverse Words in a String III

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

  9. ledecode Reverse Words in a String III

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

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

随机推荐

  1. [POJ3682]King Arthur's Birthday Celebration[期望DP]

    也许更好的阅读体验 \(\mathcal{Description}\) 每天抛一个硬币,硬币正面朝上的几率是p,直到抛出k次正面为止结束,第\(i\)天抛硬币的花费为\(2i-1\),求出抛硬币的天数 ...

  2. 编译基于obs-studio的阿里巴巴直播工具tblive的过程和常见问题解决

    tblive 简介 tblive开源项目对应的产品是千牛主播,是一个独立的PC端主播工具,基于开源软件OBS Studio来修改定制. 项目说明 tblive是一款优秀的基于obs-studio的直播 ...

  3. 【开发工具】- Xshell工具的下载和安装

    下载地址:https://www.netsarang.com/zh/free-for-home-school/ Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Micro ...

  4. <P>标签是什么?怎么用!

    <P>标签它是一个段落标签,它和<br>标签不一样.会自行起一行段落,并且可以作为一个盒子来使用.可以单独定义它. 比如下图: <p>这个就是一个段落</p& ...

  5. 轻量ORM-SqlRepoEx介绍

    轻量级 ORM-SqlRepoEx 介绍 SqlRepoEx是 .Net平台下兼容.NET Standard 2.0人一个轻型的ORM.解决了Lambda转Sql语句这一难题,SqlRepoEx使用的 ...

  6. 动态修改app build版本CFBundleVersion

    1.需求说明 2.操作步骤 2.1 新建脚本,选择Build Phases 2.2 点击加号,选择New Run Script Phase 2.3 为了便于识别,双击重命名为 Dynamic Buil ...

  7. unity 3D物体使用EventSystem响应事件

    在ugui中创建一个canvas 之后会自动创建一个EventSystem,用来处理UI上的时间响应.(可以通过UI>EventSystem创建EventSystem) EventSystem ...

  8. Python学习日记(六) 浅深copy

    浅深copy即完全复制一份和部分复制一份 浅深copy在列表数据量较大时不建议使用,比较消耗内存资源 1.赋值运算 l1 = [1,'s',[1,2,3]] l2 = l1 print(id(l1), ...

  9. Android自动化测试探索(七)代码覆盖率统计

    之前在 https://www.cnblogs.com/zhouxihi/p/11453738.html 这篇写了一种统计Android覆盖率的方式 但是对于一些比较复杂或者代码结构不够规范的项目,有 ...

  10. mysql学习之基础篇02

    我们来说一下表的增删改查的基本语法: 首先建立一个简单的薪资表: create table salary(id int primary key auto_increment,sname varchar ...