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. CSS-边框-效果

    1.1边框 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特性,我们需要重点掌握. 1.1.1边框圆角 border-radius 每个角可以设置两个值,x值,y值 ...

  2. html标签之meta标签

    1  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   ...

  3. Cloudera Manager Free Edition 4.1 和CDH 4.1.2 简易安装教学

    转载:http://fenriswolf.me/2012/12/06/cloudera-manager-free-edition-4-1-和-cdh-4-1-2-简易安装教学/ 安装及管理一个大的Ha ...

  4. 介绍一款管理软件Redmine

    Redmine是用Ruby开发的基于web的项目管理软件,是用ROR框架开发的一套跨平台项目管理系统,据说是源于Basecamp的ror版而来,支持多种数据库,有不少自己独特的功能,例如提供wiki. ...

  5. UIView 和 UIWindow 的学习内容

    UIWindow是UIView的子类,一个程序只能有一个window主窗口. 在XCode7之后我们创建UIWindow的对象,代码如下: //创建一个窗口,使其铺满屏幕(设置大小)         ...

  6. SERVERPROPERTY方法说明

    SERVERPROPERTY 返回有关服务器实例的属性信息. 语法 SERVERPROPERTY ( propertyname ) 参数 propertyname 是包含要返回的服务器属性信息的表达式 ...

  7. Java-----判断是否为基本类型

    转载自:http://blog.csdn.net/hekewangzi/article/details/51969774

  8. IE8的项目在IE11下 一些功能无法实现的解决方案

    最近改了一些IE11下一些功能无法实现的项目,发现了有一些IE8下的方法 ,在IE11下被取消或者替代了,如下: 1.JavaScript 运行时错误: 对象不支持“attachEvent”属性或方法 ...

  9. ThinkPHP 类似Yii的Gii生成Model的功能。

    ThinkPHP 类似Yii的Gii生成Model的功能.自动生成ThinkPhp 3.1 的基础模型.. #!/usr/bin/env php <?php /** * * THINKPHP 基 ...

  10. Git学习02 --暂存区,撤销修改,删除文件

    工作区和暂存区概念: 工作区(Working Directory)就是你在电脑里能看到的目录. 版本库(Repository) 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库. Gi ...