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

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

For C programmers: Try to solve it in-place in O(1) space.

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.
这个题如果用C++的stringstream字符串流来实现的话,会非常简单
void reverseWords(string &s) {
istringstream is(s);
string tmp;
is >> s;
while(is >> tmp) s = tmp + " " + s;
if(s[] == ' ') s = "";
}

但是,题目中对于C语言实现有更严格的要求,需要就地操作

联想到之前碰到的题目,数组旋转问题,我们是否可以用类似的思路来解决呢

对于每一个单词,都将其字母前后逆置,然后再将整个字符串逆置,这样就可以得到正确反转结果了。

但是如何处理多余的空格呢?我们可以在遍历的同时,利用快慢双指针来将空格省略掉。然后再执行逆置操作。

代码如下:

void reverseWords(string &s) {

    int i = , j = ;
int l = ;
int len = s.length();
int wordcount = ; while (true) {
while (i<len && s[i] == ' ') i++;
if (i == len) break;
if (wordcount) s[j++] = ' ';
l = j;
while (i<len && s[i] != ' ') { s[j] = s[i]; j++; i++; }
reverseword(s, l, j - );
wordcount++;
}
s.resize(j);
reverseword(s, , j - );
}

Reverse Words in a String leetcode的更多相关文章

  1. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  2. Reverse Words in a String——LeetCode

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

  3. Reverse Words in a String leetcode java

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

  4. Reverse Words in a String | LeetCode OJ | C++

    我的思路:先读取每一个单词,存放到容器中:读取完毕后,将容器中的单词倒序写入输出中. #include<iostream> #include<string> #include& ...

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

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

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

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

  8. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  9. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

随机推荐

  1. Android3.0 以前的Fragment支持

    Fragment非常实用,Android也为3.0以前的平台增加了Fragment支持,只是该Fragment不是继承android.app.Fragment,而是继承android.support. ...

  2. poj 1013(uva 608) Counterfeit Dollar

    #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...

  3. ThinkPHP--IS_AJAX

    增加IS_GET,IS_POST,IS_PUT,IS_DELETE,IS_AJAX常量,方便除控制器外的地方判断方法,Action类的isGet isPost等方法暂时保留,但不建议使用.

  4. VAST3.0规范

    VAST3.0视频广告投放规范 Posted on 2014年2月15日 1.术语 随着视频广告行业的发展,某些术语已经得到了广泛的采用.以下定义该文档中与视频广告投放相关的一些术语: 广告荚(Ad ...

  5. 如何从本地把项目上传到github&&如何把github项目通过clone复制下来

    一.第一步---注册一个Github账号 首先要在GitHub上创建一个帐号,可以去官方网站注册一个账号. 二.git安装 下载地址:http://msysgit.github.io/ 二.第二步-- ...

  6. Chrome 插件集锦

    原文出处:CN_Simo 子曾曰:"工欲善其事,必先利其器.居是邦也."--语出<论语·卫灵公>:其后一百多年,荀子也在其<劝学>中倡言道:"吾尝 ...

  7. jstree获取整棵树的json数据

    $("#树ID").jstree(true).get_json();

  8. 浅谈Django的Q查询以及AngularJS的Datatables分页插件

    使用Q查询,首先要导入Q模块: from django.db.models import Q 可以组合使用&,|操作符用于多个Q的对象,产生一个新的Q对象,Q对象也可以用~操作符放在前面表示否 ...

  9. (原)SQL Server 系统提供功能的三个疑惑

    本文目录列表: 1.SQL Server系统提供的部分疑惑概述2.系统函数调用时DEFAULT代替可选参数使用不统一3.队列字段列message_enqueue_time记录的是UTC日期时间 4.@ ...

  10. mybatis中mapping文件like查询方式

    方式1: select * from person where name like "%"#{name}"%" 方式2: select * from perso ...