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 ...
随机推荐
- asp.net core 一个中小型项目实战的起手式——Swagger配置
交流群:863563315 一.Swagger是什么 Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 二.如何在项目中加入Swagger ...
- (一) CentOS 7 进行 Docker CE 安装
参考并感谢 官方文档: https://docs.docker.com/install/linux/docker-ce/centos/ 卸载旧版本 # 停止所有正在运行的容器 docker stop ...
- isolate sqflite demo
main.dart import 'package:flutter/material.dart'; import 'demo_isolates.dart'; import 'package:rxdar ...
- stm32 USART_IT_IDLE中断 一帧数据
USART_IT_IDLE中断,是串口收到一帧数据后,发生的中断.也可以叫做一包数据 USART_IT_IDLE和USART_IT_RXNE区别 当接收到1个字节,会产生USART_IT_RXNE中断 ...
- PHP-5.6.8 源码包编译安装
一.下载源码包后,进行解压 [root@www home]# .tar.bz2 gzip: stdin: not in gzip format tar: Error is not recoverabl ...
- javascript中的var,let,const关键字
文章:JavaScript 中 var 和 let 和 const 关键字的区别 比较全面的文章.
- python中的exec和eval
exec 描述 exec 执行储存在字符串或文件中的 Python 语句,相比于 eval,exec可以执行更复杂的 Python 代码. 返回值 exec 返回值永远为 None. 需要说明的是在 ...
- python算法与数据结构-插入排序算法(34)
一.插入排序的介绍 插入排序的工作方式非常像人们排序一手扑克牌一样.开始时,我们的左手为空并且桌子上的牌面朝下.然后,我们每次从桌子上拿走一张牌并将它插入左手中正确的位置.为了找到一张牌的正确位置,我 ...
- wordpress实现主动推送+熊掌号推送同步进行
今天给一个朋友http://www.myunigift.cn/ 这个站点是用wordpress,今天帮他改造熊掌号,于是做了数据同步推送. 只要把下面的代码写到funtions.php里面,发布文章的 ...
- mysql 截取字符串 函数
文章摘取自http://www.cnblogs.com/zdz8207/p/3765073.html 练习截取字符串函数(五个) mysql索引从1开始 一.mysql截取字符串函数 1.left(s ...