Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Have you met this question in a real interview?

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

LeetCode上的原题,请参见我之前的博客Reverse Words in a String

解法一:

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
int storeIndex = , n = s.size();
reverse(s.begin(), s.end());
for (int i = ; i < n; ++i) {
if (s[i] != ' ') {
if (storeIndex != ) s[storeIndex++] = ' ';
int j = i;
while (j < n && s[j] != ' ') s[storeIndex++] = s[j++];
reverse(s.begin() + storeIndex - (j - i), s.begin() + storeIndex);
i = j;
}
}
return string(s.begin(), s.begin() + storeIndex);
}
};

解法二:

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
string res = "", t = "";
istringstream is(s);
while (getline(is, t, ' ')) {
if (t.empty()) continue;
res = (res.empty() ? t : (t + " " + res));
}
return res;
}
};

解法三:

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
istringstream is(s);
is >> s;
string t;
while (is >> t) {
s = t + " " + s;
}
return (s[] == ' ') ? "" : s;
}
};

[LintCode] Reverse Words in a String 翻转字符串中的单词的更多相关文章

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

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

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

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

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

  4. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  5. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  6. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  7. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  8. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  9. 151. Reverse Words in a String翻转一句话中的单词

    [抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...

随机推荐

  1. 使用JVMTI创建调试和监控代理

    Java 虚拟机工具接口(JVMTI)提供了一个编程接口,允许你(程序员)创建software agent 来监视和控制你的Java应用. JVMTI 代替了原来的Java Virtual Machi ...

  2. tyvj1172 自然数拆分Lunatic版

    背景 话说小小鱼看了P1171(自然数拆分)之后感觉异常不爽,于是异常邪恶地将题目加强. 描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入格式 输入只有一个整数n ...

  3. C#深入浅出 继承(六)

    这个标题写出来好长时间了,都没写内容,今天无论如何都得写完它,昨天写了一段,尼玛,电脑突然死机,重启之后啥都没了. 继承嘛,有人觉得很简单,但是里面还是有内容可以讲的,因为面向对象也就这么点内容,封装 ...

  4. storyboard连线容易出现的问题

    - 连接的方法代码被删掉,但是连线没有去掉 - 可能会出现方法找不到错误 - unrecognized selector sent to instance- 连接的属性代码被删掉,但是连线没有去掉 - ...

  5. UI第十一节——UIActivityIndicatorView

    - (void)viewDidLoad {    [super viewDidLoad];        // 创建一个UIActivityIndicatorView,大小是固定的    UIActi ...

  6. java生产者/消费者模式实现——一生产者一消费者(操作值)

    胶多不粘话多不甜,直接上代码: 生产者类: /** * Created by 51304 on 2016/2/28. */ public class P { private String lock; ...

  7. Daily Scrum Meeting ——ThirdDay

    一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.完成了github上的文档整理 Transcend/ActivityHelper 2.主界面侧滑框的 ...

  8. Linux下c开发 之 线程通信(转)

    Linux下c开发 之 线程通信(转) 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linu ...

  9. c语言中的一些注意点

    1.头文件两种形式的区别(#include<mystring.h>与#include"mystring.h") 当运行一个程序时,需要调用自己写的函数时,需要在头文件加 ...

  10. Centos7设置IP为固定值

    1.进入到系统的IP地址保存文件所在目录 [root@localhost ~]# cd /etc/sysconfig/network-scripts 2.修改保存IP信息的文件 [root@local ...