Reverse Words in a String leetcode
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
For C programmers: Try to solve it in-place in O(1) space.
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
void reverseWords(string &s) {
istringstream is(s);
string tmp;
is >> s;
while(is >> tmp) s = tmp + " " + s;
if(s[] == ' ') s = "";
}
但是,题目中对于C语言实现有更严格的要求,需要就地操作
联想到之前碰到的题目,数组旋转问题,我们是否可以用类似的思路来解决呢
对于每一个单词,都将其字母前后逆置,然后再将整个字符串逆置,这样就可以得到正确反转结果了。
但是如何处理多余的空格呢?我们可以在遍历的同时,利用快慢双指针来将空格省略掉。然后再执行逆置操作。
代码如下:
void reverseWords(string &s) { int i = , j = ;
int l = ;
int len = s.length();
int wordcount = ; while (true) {
while (i<len && s[i] == ' ') i++;
if (i == len) break;
if (wordcount) s[j++] = ' ';
l = j;
while (i<len && s[i] != ' ') { s[j] = s[i]; j++; i++; }
reverseword(s, l, j - );
wordcount++;
}
s.resize(j);
reverseword(s, , j - );
}
Reverse Words in a String leetcode的更多相关文章
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- Reverse Words in a String——LeetCode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- Reverse Words in a String | LeetCode OJ | C++
我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
随机推荐
- spring mvc 下载文件链接
http://www.blogjava.net/paulwong/archive/2014/10/29/419177.html http://www.iteye.com/topic/1125784 h ...
- inline「一」:从 image 底部白边初识 line-height
本文首发于个人博客 http://www.lijundong.com/image-and-line-height/ 今天在做一个静态页面时,图片底部出现一条 3px 高度的白边,既不是 margin ...
- spring-dwr注解整合
注解配置 1.web.xml 只需将DwrServlet换为DwrSpringServlet(包名不同) 2.dwr类 3.applicationContext.xml 4.annotationCon ...
- SQL Server事务、隔离级别详解(二十九)
前言 事务一直以来是我最薄弱的环节,也是我打算重新学习SQL Server的出发点,关于SQL Server中事务将分为几节来进行阐述,Always to review the basics. 事务简 ...
- 支付宝ios支付请求Java服务端签名报的一个错(ALI40247) 原创
今天做app的支付宝支付,遇到些问题,以前做支付宝支付签名都是直接在客户端App进行,今天下了最新版本ios的支付宝支付demo,运行demo时底部有红色的显眼字体,告知用户签名必须在服务端进行... ...
- HDU 1006 [Tick Tick]时钟问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1006 题目大意:钟表有时.分.秒3根指针.当任意两根指针间夹角大于等于n°时,就说他们是happy的, ...
- linux环境下搭建 j2ee环境
一.JDK安装(安装在/usr/java目录下)1.下载:jdk-7-ea-bin-b26-linux-i586-24_apr_2008.bin地址:http://jx.newhua.com/down ...
- C++虚表(V-Table)解析
C++中的虚函数的作用主要是实现了多态,本人通过代码验证的方式了解虚表的结构及在多种继承方式下通过虚表访问子类函数.验证结果如下: 1)无虚函数覆盖的一般继承:可以通过子类的虚表访问父类的函数 2)虚 ...
- Boost.Hana在visual studio 2017 rc中的残缺使用
最新的visual studio还不支持hana,不知道vs2017正式版本出后会不会支持.等不及了,先用rc版试试吧. 1.从https://github.com/boostorg/hana下载或拉 ...
- 关于最优化读写,测试各个加锁:Lock、安全字典、信号量、ReaderWriterLock、ReaderWriterLockSlim
大家对于自身项目中都有本地缓存的逻辑,但这块的读写何种机制下哪个快,哪个慢,需要测试对比,以下测试报告,期望给大家一个借鉴,如果有更好的测试结果也欢迎大家一起讨论. 以下测试是开启10个并发任务对同一 ...