leetCode题解 Reverse Words in a String III
1、题目描述
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.
输入一个string 结构的句子,单词之间使用一个空格隔开(' '),将句子中的每个单词单独反转。保持反转之后每个单词在句子中的位置。
2、问题分析
每个单词之间使用空格隔开,遍历一次string ,寻找空格,然后反转该空格之前的单词,一直到结尾。
3、代码
string reverseWords(string s) {
s = s+' '; // 给string 后面添加一个空格,用于反转最后一个单词。
auto b = s.begin(); // C++11 特性,迭代器
auto e = s.end();
auto p =s.begin(); // 这个迭代器在每次反转之后更新,指向最近一个没有被反转的单词
for(b = s.begin(); b != e; ++b)
{
if(*b == ' ')
{
reverse(p,b); // C++ 中 algorithms 中的函数,用于反转。
p = b + ; // 更新 迭代器 p
}
}
s.erase(s.end() -); // 去除添加在string最后的空格
return s;
}
leetCode题解 Reverse Words in a String III的更多相关文章
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 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 ...
- [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 ...
- Leetcode - 557. Reverse Words in a String III (C++) stringstream
1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...
- 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 ...
- 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 ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- SPSS学习系列之SPSS Text Analytics是什么?
不多说,直接上干货! IBM® SPSS® Text Analytics 是一个IBM® SPSS® Modeler 完全集成内插式插件,它采用了先进语言技术和Natural Language Pro ...
- Nodejs学习笔记(十三)—PM2
简介 PM2 pm2是一个内置负载均衡的node.js应用进程管理器(也支持Windows),其它的类似功能也有不少,但是感觉pm2功能更强,更值的推荐 GitHub地址:https://github ...
- ConfigurationManager
ConfigurationManager读取和写入 提供对客户端应用程序配置文件的访问 通过引入System.Configuration.dll可以用ConfigurationManager类来读取项 ...
- Apache Thrift - 可伸缩的跨语言服务开发框架 ---转载
src:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ http://thrift.apache.org/
- Django控制器
配置路由 通过对urls.py的配置将用户请求映射到处理函数. Django的URL字符串匹配实际上基于正则表达式,这允许单条URL可以匹配一类请求.参见Django Book中的示例: from d ...
- 消息队列系列(二):.Rabbitmq的使用及Web监控工具使用
一.文档资料 1.官方网站:http://www.rabbitmq.com/ 2.安装教程:http://www.rabbitmq.com/install-windows. ...
- WCF配置文件与文件下载之坎坷路
题外话:本以为我会WCF了,精通WCF了,毕竟刚做过一个WCF的项目,不就是写写契约接口,然后实现接口,改下配置.最后用控制台或者服务发布一下,不就能用了.不就是简单ABC吗?不是So Easy吗?做 ...
- JavaScript部分兼容性函数
1.getElementsByClassName() function getElementsByClassName(node,classname){ if(node.getElementsByCla ...
- ASP.NET 关于GridView 表格重复列合并
这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下GridView 和 Repeater 关于重复数据合并的方法. 效果图如下 : GridView : 前台代码 : ...
- 阿里云搭建hadoop集群服务器,内网、外网访问问题(详解。。。)
这个问题花费了我将近两天的时间,经过多次试错和尝试,现在想分享给大家来解决此问题避免大家入坑,以前都是在局域网上搭建的hadoop集群,并且是局域网访问的,没遇见此问题. 因为阿里云上搭建的hadoo ...