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. Selenium IE6 Failed to load the library from temp directory: C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\IED1C1.tmp

    项目中用到了使用Selenium打开IE浏览器.前期都是基于IE8+.Firefox.Chrome.项目后期开始现场测试时发现大部分客户还都是使用的Windows XP + IE6.结果可想而知,直接 ...

  2. 如何将php数组或者对象传递给javascript

    在网上搜了一些方法,最后自己采用的是通过json字串的方式. 假设有一个php 数组 $arr,代码如下: 代码如下: <script> // html5中默认的script是javasc ...

  3. Java线程:线程状态

    线程可以创建6状态: New()新创建.Runnable(可运行).Blocked(被阻塞). Waiting(等待).Timed waiting(计时等待).Terminated(被终止)1 新建线 ...

  4. #DP# ----- OpenJudge最大子矩阵

    OpenJudge 1768:最大子矩阵 总时间限制: 1000ms   内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 ...

  5. 在Flex中使用文件系统

    一.File和FileStream对象File和FileStream是AIR文件系统中,重要的组成部分,File对象有许多属性,用于唯一区别它与文件系统上的其他文件对象,属性包括:url/native ...

  6. [MyBatis]mapperLocations属性通配符的使用

    http://blog.csdn.net/szwangdf/article/details/23432783 http://ljhzzyx.blog.163.com/blog/static/38380 ...

  7. Eclipse中TODO的分类,以及自动去除

    Window-Preference-Java-Compiler-Task Tags; 这里面进行TaskTag标签的定义,默认支持FIXME.TODO.XXX三种:优先级高的在taskview中 会显 ...

  8. bzoj1492--斜率优化DP+cdq分治

    显然在某一天要么花完所有钱,要么不花钱. 所以首先想到O(n^2)DP: f[i]=max{f[i-1],(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j])},j& ...

  9. Angular企业级开发(7)-MVC之控制器

    1.MVC中的控制器 AngularJS的控制器主要为了把模型和视图连接在一起.大多数业务逻辑操作都会放在视图对应的控制器中.当然如果我们能够把业务逻辑放到后端的REST服务中,就可以开发轻量级Ang ...

  10. Oracle RAC学习笔记02-RAC维护工具集

    Oracle RAC学习笔记02-RAC维护工具集 RAC维护工具集 1.节点层 2.网络层 3.集群层 4.应用层 本文实验环境: 10.2.0.5 Clusterware + RAC 11.2.0 ...