[string]Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Update (2015-02-12):
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.
class Solution {
public:
void formatWords(string& s)
{
int sSize = s.size();
int i=,j=,k=sSize;
while(i<sSize && s[i]==' ') i++;
while(k>= && s[k-]==' ') k--;
bool isFirstSpace = true;
while(i<k){
if(s[i] != ' '){
isFirstSpace = true;
s[j++] = s[i++];
continue;
}
if(isFirstSpace){
s[j++] = ' ';
isFirstSpace = false;
}
i++;
}
s.erase(s.begin()+j,s.end());
}
void reverseStr(string &s,int startPos,int endPos)
{
while(startPos<endPos){
swap(s[startPos++],s[--endPos]);
}
}
void reverseWords(string &s) {
formatWords(s);
int sSize = s.size();
reverseStr(s,,sSize);
int i=,startPos = ;
bool isFirstAlph=true;
for(;i<sSize;i++){
if(s[i]==' '){
reverseStr(s,startPos,i);
isFirstAlph = true;
continue;
}
if(isFirstAlph){
startPos = i;
isFirstAlph = false;
}
}
reverseStr(s,startPos,i);
}
};
[string]Reverse Words in a String的更多相关文章
- [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 OJ1:Reverse Words in a String
问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- leetcode Online Judge 150题 解答分析之一 Reverse Words in a String
问题 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java
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(hard)☆
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
随机推荐
- Html5 代码
随着HTML5的流行,许多网站开始介绍HTML5元素和属性的用法,以及各种教程,并且越来越多老的浏览器开始兼容HTML5. 本文作者编译了10段非常实用的HTML5代码片段,开发者可以直接拿过去使 ...
- application,session,cookie三者之间的区别和联系
application: 程序全局变量对象,对每个用户每个页面都有效 session: 用户全局变量,对于该用户的所有操作过程都有效 session主要是在服务器端用,一般对客户端不 ...
- XPath语法 在C#中使用XPath示例
XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: <?xml version="1.0" en ...
- 解决linux top命令提示的unknown terminal type的问题
[root@localhost bin]# top 'xterm-256color': unknown terminal type. 在网上搜索了解决方法如下: 解决办法: 1.临时办法,下次启动失效 ...
- 使用SQLiteHelper创建数据库并插入数据
参考<疯狂android讲义>8.4节P424 1.获取SQLiteDatabase实例有2种方法,一是直接new SQLiteDatabase(),另一种使用SQLiteHelper.一 ...
- Gallery过时替代方案HorizontalScrollView
布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:androi ...
- ListView的小知识
1.设置项目分割功能 android:divider="@android:color/blue" android:dividerHeight="10dp" &l ...
- Kali Linux 新手折腾笔记
http://defcon.cn/1618.html 2014年09月29日 渗透测试 暂无评论 阅读 55,052 次 最近在折腾Kali Linux 顺便做一简单整理,至于安装就不再多扯了,估 ...
- JS面向对象编程之:封装、继承、多态
最近在实习公司写代码,被隔壁的哥们吐槽说,代码写的没有一点艺术.为了让我的代码多点艺术,我就重新温故了<javascript高级程序设计>(其中几章),然后又看了<javascrip ...
- Qt :非window子窗体的透明度设置
✿问题的由来 心血来潮,想利用QTimer 配合 setWindowOpacity()方法来实现一个窗体淡入的效果. ✿实验代码 粗糙的实验代码: void Widge ...