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. C#基础学习心得(二)

    索引器 class Program { static void Main(string[] args) { Employee e1 = new Employee(); e1[0] = "三& ...

  2. CMD下修改IP地址

    @echo off netsh interface ip set address name="本地连接" static 192.168.1.55 255.255.255.0 192 ...

  3. jdbc读取数据库图片文件

    package 读取大文件.read; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.I ...

  4. linux安装chrome

    wget http://chrome.richardlloyd.org.uk/install_chrome.sh chmod u+x install_chrome.sh ./install_chrom ...

  5. ProxyCreationEnabled 和 LazyLoadingEnabled 的关系

    我们会使用这两个设置来决定是否加载导航属性.默认情况这两个值都是true的,也就是说会以延迟加载的方式加载导航属性,也就是当我们访问导航属性的时候才会去查数据库获取导航属性的数据.db.Configu ...

  6. eclipse ldt update resource

    http://download.eclipse.org/ldt/releases/milestones/ 百度一下都说是 http://download.eclipse.org/koneki/upda ...

  7. [转]标准C++字符串string以及MFC6.0字符串CString的tokenize和split函数

    标准字符串的方法: /******************************************** the tokenize function for std::string ****** ...

  8. C学习-fgets()篇1

    学习fgets()函数时发现了一个问题,先贴代码 #include<stdio.h> #include<string.h> #include<ctype.h> vo ...

  9. Mysql JOIN优化。

    join性能自行百度,google 数据60w+,这里我只测试了一个limit , ) ,) AS C LEFT JOIN table2 AS B ON C.e_id=B.id; ) ,;

  10. 【ecos学习5】redboot 加载运行hello world

    背景: 从主机 192.168.2.14 IP,下载bin文件hello到ecos. redboot>load -v -h 192.168.2.14 hello Using default pr ...