leetcode解题报告(25):Reverse Words in a String III
描述
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的更多相关文章
- [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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 设置session销毁时间
currentUser.getSession().setTimeout();
- [SOJ #538]好数 [CC]FAVNUM(2019-8-6考试)
题目大意:给定$n$个正整数,求$[l,r]$中第$k$小的”好数“.$l,r\leqslant10^{18},n\leqslant62$,出现的其他数均$\leqslant10^{50}$ 好数定义 ...
- new Image 读取宽高为0——onload
获取图片一张图片的大小 let img = new Image() img.src = imgUrl if ( img.width != 375 || img.height != 200 ) { me ...
- WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null
原文:WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null 在 WPF 程序中,可能会存在 Application.Curren ...
- node.js开发 npm包管理工具 npm 和 cnpm区别
npm 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用. 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用 np ...
- iOS - 安装CocoaPods详细过程(重装系统后!)
重装的系统,发现很多东西都要重装,顺便复习和检验下以前的方法还有没有效 一.简介 什么是CocoaPods CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们 ...
- 【雅思】【绿宝书错词本】List37~48
List 37 ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ List 38 ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ ❤ List 39 ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ...
- 笔谈FFmpeg(二)
经过前面的学习对FFmpeg的基本流程已经很熟悉了,现在到了掌握其中细节的时候了,用FFmpeg做播放器解码操作中,涉及到了一些结构体,这些结构之间到底有什么关系,它们是怎样协同工作的呢.文章 FFM ...
- 转 Python3 ssl模块不可用的问题
编译安装完Python3之后,使用pip来安装python库,发现了如下报错: $ pip install numpy pip is configured with locations tha ...
- RobotFramework+Eclipse的安装和配置(一)
最近想学robotframwork来做自动化,那立马就来开始上手 想动手,起码要先下载工具,工具及框架 工具介绍 Robotframework:一款自动化测试框架. Eclipse:一款编辑工具,可以 ...