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.

click to show clarification.

Clarification:

  • 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. LeetCode OJ1:Reverse Words in a String

    问题描述: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

  5. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. 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 ...

  7. 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 ...

  8. 【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& ...

  9. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

随机推荐

  1. 远程调试weinre的使用

    一.用途 *鉴于在浏览器调试移动端页面无法准确反映移动端实际情况并无法高效调试,故常常使用远程调试工具通过电脑连接手机进行调试,常用远程调试方式: 1.chrome连接安卓机远程调试 2.Mac连接苹 ...

  2. SQL SERVER中如何格式化日期(转)

    原文地址:http://blog.sina.com.cn/s/blog_95cfa64601018obo.html   1. SELECT convert(varchar, getdate(), 10 ...

  3. Direct2D WIC绘制图片

    绘制图片需要用到WIC,WIC的功能包括: 编解码图片.也可以自定义图片解码插件. 读取图片元数据. 图像处理(最高支持每通道32位). 内置支持一些流行的格式.包括:BMP v5, GIF 89a/ ...

  4. oracle数据块的大小

    标准数据块用于临时表空间和系统表空间,同时也是一个表空间数据块的默认值.标准数据块的大小是在创建数据库时由参数DB_BLOCK_SIZE确定的.若要改变这一设置必须重建数据库. DB_CACHE_SI ...

  5. UVa230 Borrowers (STL)

     Borrowers  I mean your borrowers of books - those mutilators of collections, spoilers of the symmet ...

  6. SQL Server 锁的 8 种类型

    第1种. 共享锁.由读取查寻产生. 第2种. 意向锁.用意向锁来表示有将要获得某一资源的意向. 第3种. 更新锁.在修改数据前获得. 第4种. 排它锁.用于独占某一资源时获得. 第5种. 架构锁.运行 ...

  7. SQL Server 错误18456

    第一步. 错误发生的场景 第二步. 找到引起错误的原因 第1步. 查看windows日志文件. 运行中输入 eventvwr (event viewer)打开日志文件查看器, 第三步. 解决方案,由第 ...

  8. iis 回收工作进程时出错的解决办法

    第一种解决方案: iis6系统默认的工作进程回收时间是29个小时有很多问题是在回收工作进程后出现很多问题如典型的500错误等经过我做服务器的一段时间的观察大家可以不用回收工作进程而是把应用程序池的最大 ...

  9. linux之SQL语句简明教程---CREATE VIEW

    视观表 (View) 可以被当作是虚拟表格.它跟表格的不同是,表格中有实际储存资料,而视观表是建立在表格之上的一个架构,它本身并不实际储存资料. 建立一个视观表的语法如下: CREATE VIEW & ...

  10. Android学习之SharedPreferences类

    SharedPreferences类 android.content.SharedPreferences 类概括: 访问和修改由函数getSharedPreferences(String,int)返回 ...