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. 一.Linq to JSON是用来干什么的?

    Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JSON对象内容比较复杂,而我们仅仅需要其中的一小部分数据时,可以考虑使用Linq to JSON来读取和 ...

  2. 检查DISPLAY设置时Xlib出现No protocol specified错误

    退出到root用户,执行xhost +命令后,再次切换到Oralce用户,执行runInstaller命令,错误消失

  3. vs2010 中检测到有潜在危险的 Request.Form 值

    解决方法 : 一般在网上搜只有以下两种处理方式: 1.在报错的页面前吧<%Page%>标签中增加validateRequest="false"的属性为false 如下所 ...

  4. 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)

    最近搞一个自己的博客系统玩,用了servlet+jsp,结果发现了两个大问题: 1.无法 Export 出 WAR文件: 2.生成WAR,放置到TOMCAT的 webapps目录后,http://lo ...

  5. iOS正则表达式的使用

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. SQL Server dbcc shrinkfile 不起作用

    方法 1.重建聚集索引. 方法 2.重建堆表. ---------------------------------------------------------------------------- ...

  7. SSAS维度设计中CustomRollupColumn的用法-自定义聚合方式

          CustomRollupColumn说明:指定包含多维表达式的列,该表达式可用于聚合特性的度量值.这个属性覆盖给定度量值的AggregateFunction的属性. 解释:通常我们的度量值 ...

  8. JVM的生命周期——JVM之二

    一.首先分析两个概念 JVM实例和JVM执行引擎实例 (1)JVM实例对应了一个独立运行的java程序——进程级别 一个运行时的Java虚拟机(JVM)负责运行一个Java程序. 当启动一个Java程 ...

  9. 一道C语言面试题:写一个宏,将16位的整数转为Big Endian

    题目:输入16位整数x,如0x1234,将其转为Big Endian格式再输出,此例为输出 0x3412 来源:某500强企业面试题目 思路:将x左移8位得到a,将x右移8位得到b,a+b即为所得 / ...

  10. Inno Setup 打包工具总结(转)

    最近打包用到了Inno setup,在这个过程中容易犯一些低级错误,特别写出来已提醒自己 1.打包文件夹 打包文件按照向导来一般没什么问题,但文件夹就不一样了.向导生成的打包文件夹的代码如下: Sou ...