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 ...
随机推荐
- MySQL生产库全库备份脚本
创建一个单独的备份用户backup,不要用root 创建备份目录 :mkdir -p /databackup/fullbackup mysql> grant SELECT,RELOAD,SHOW ...
- Flash对不同的浏览器的兼容性
现在遇到两个Flash的兼容性问题: 1.找不到指定的摄像头(VCamera),可是该摄像头在QQ等IM工具中是可以正常使用的 2.能找到摄像头,不过他的预览是黑屏 针对第一个问题: a.采用IE内核 ...
- doubango(5)--SIP协议栈传输层的启动
SIP协议的INVITE消息发起流程 当通过sip协议发起一个会话时,需要通过invite消息实现该流程.而SIP协议是一个基于事务的协议,每一个sip会话的都是通过sip部件间的一系列消息来完成的. ...
- js动画(四)
终于到了最后了,这里要告一段落了,整了个js运动框架,咳咳咳,好冷 啊啊啊啊啊啊,这天气.妈的,工资怎么也不发,啊,说好的 人与人之间的信任呢?哎,气诶,不到150字啊,又是这个梗..怎么办?说些什么 ...
- 关于多字节字符入库失败处理(所谓的Emji),该处理是舍弃特殊字符
具体处理方法及样例如下: /** * 屏蔽超过三个字节以上的字符 * @param strByte * @return */ public static String filterUtf8(byte[ ...
- java gc的调用机制 和编程规则
转载:http://sunzhyng.iteye.com/blog/480148 一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,有一些应用程序对性能要求 ...
- SSM框架注解整合
一.web应用环境 1.ServletContext 对于一个web应用,其部署在web容器(比如:tomcat)中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext, ...
- 搭建typescript开发环境最详细的全过程
搭建typescript开发示例https://github.com/Microsoft/TypeScriptSamples typescript案例https://www.tslang.cn/sam ...
- 《JAVASCRIPT高级程序设计》闭包
一.闭包的概念 闭包是JAVASCRIPT中最重要的概念之一,闭包是指有权访问另一个函数作用域中变量的函数:创建闭包常见的方式,就是在一个函数内部,创建另一个函数.以下的例子创建了一个闭包,加粗的两行 ...
- java原装代码完成pdf在线预览和pdf打印及下载
这是我在工作中,遇到这样需求,完成需求后,总结的成果,就当做是工作笔记,以免日后忘记,当然,能帮助到别人是最好的啦! 下面进入正题: 前提准备: 1. 项目中至少需要引入的jar包,注意版本: a) ...