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 ...
随机推荐
- Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门
Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...
- IntelliJ IDEA的黑白色背景切换(Ultimate和Community版本皆通用)
不多说,直接上干货! IntelliJ IDEA的黑白色背景切换 File -> Setting -> Editor -> Colors & F ...
- VMware里Ubuntukylin-14.04-desktop的VMware Tools安装图文详解
不多说,直接上干货! 总的来说,根据分为三个步骤. 步骤一: 点击 :虚拟机—–>安装VM tools 然后发现桌面会跳出如下问题: 客户机操作系统已将 CD-ROM 门锁定,并且可能正在使用 ...
- 软件魔方制作系统启动盘并安装win8系统
不多说,直接上干货! 推荐软件:软件魔方 http://mofang.ruanmei.com/ 这里,我想说的是,这个软件来制作系统盘,是真的方便和好处多多.具体我不多说,本人也是用过其他的如大白菜等 ...
- 小游戏专场:腾讯云Game-Tech技术沙龙上海站顺利落下帷幕
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云发表于云+社区专栏 9月14日腾讯云GAME-TECH技术沙龙小游戏专场在上海顺利举办,此次技术沙龙由腾讯云的资深专家,以及 ...
- 【转】30+有用的CSS代码片段
来自:WEB资源网 链接:http://webres.wang/31-css-code-snippets-to-make-you-a-better-coder/ 原文:http://www.desig ...
- EMC,EMI,EMS,ESD分别是什么?有什么区别和联系?
一.EMC EMI EMS定义: EMC(ElectromagneticCompatibility) 电磁兼容,是指设备或系统在电磁环境中性能不降级的状态.电磁兼容,一方面要求系统内没有严重的干扰源, ...
- [转]Dotfuscator 使用图解教程
本文转自:https://www.cnblogs.com/xiezunxu/articles/7228741.html Dotfuscator:是.NET混淆器和压缩器,它可以帮助您防止您的应用程序被 ...
- CSS3 transition 过度
一个元素在不同的状态之间进行平滑的交换 CSS3中使用transition属性实现过度效果 一个简单的例子: img{ background-image:url("img/1.jpg&quo ...
- 转载:SQL中的case when then else end用法
SQL中的case when then else end用法 来源: http://www.cnblogs.com/prefect/p/5746624.html Case具有两种格式.简单Case函数 ...