一、题目:

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

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

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.

原题地址

二、解题思想

翻转字符串中的单词顺序,这是个老题目了。可是leetcode上面的要求更为严格。如:

要求把开头和结尾的空格删除掉;

缩减单词间的空格数为1(假设有多个空格)。

单词若全是空格,则返回一个空字符串("").

此题思想不难。主要是注意上面三个要求和一些细节就能够AC。
大致分为两步:一个是常规的翻转字符串中的单词;还有一个就是想方法去掉串中的多余的单词;这两步骤的顺序能够颠倒。

以下给出两份代码。第一个代码是先去掉多余的空格。然后在翻转;第二个代码先翻转,在去掉多余的空格。就效率上来说应该是第一个代码的效率更高。

三、代码实现

代码一:
class Solution {
public:
void reverseWords(string &s) {
if(s.size()<=0) return ;
char *work = new char[s.size()+1];
//reduce blank
int j=0;
for(int i=0; s[i]!='\0'; ++i){
if(i>0 && s[i] == ' ' && s[i-1]!= ' ')
work[j++] = s[i];
else if(s[i] != ' ')
work[j++] = s[i];
}
if(j>0 && work[j-1]==' ')
work[--j] = '\0';
else
work[j] = '\0';
//reverse all string
reverse(work, 0, j-1);
int p= 0, i=0;
//reverse each word
while(i<j){
while(p<j && work[p]!=' ') p++;
reverse(work, i, p-1);
i = p+1;
p = i;
}
string temp(work);
s = temp;
} void reverse(char *s, int beg, int end){
while(beg < end){
char temp = s[beg];
s[beg++] = s[end];
s[end--] = temp;
}
}
};
代码二:
class Solution {
public:
void reverseWords(string &s) {
int n = s.size();
if(n<=0) return;
//if(n==1)
//reverse the whole string
reverse(s, 0, n-1);
//reverse each word
int begin=0, end = 0;
while(begin<n){
while( begin< n && s[begin] == ' ') ++begin;
end = begin;
while( end<n && s[end] != ' ') ++end;
reverse(s, begin, end-1);
begin = end;
}
//reduce blank
begin = 0;
while(begin<n && s[begin] ==' ') ++begin;
if(begin == n) {s = s.substr(0,0);return;} end = n-1;
while(end>=0 && s[end] == ' ') --end; int start = 0;
char pre;
for(; begin<=end; ++begin){
if(s[begin] != ' '){
s[start++] = s[begin];
pre = s[begin];
}else{
if(pre != ' '){
s[start++] = ' ';
pre = ' ';
}
}
}
if(start != n) s = s.substr(0, start);
} void reverse(string &s, int begin, int end){
char temp;
while(begin<end){
temp = s[begin];
s[begin++] = s[end];
s[end--] = temp;
}
}
};

假设你认为本篇对你有收获。请帮顶。

另外,我开通了微信公众号--分享技术之美。我会不定期的分享一些我学习的东西.

你能够搜索公众号:swalge 或者扫描下方二维码关注我
(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/28236933
)

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

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

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

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

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

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

  5. LeetCode Reverse Words in a String II

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

  6. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  7. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  8. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  9. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  10. [leetcode]Reverse Words in a String @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...

随机推荐

  1. MathType插件安装

    1 安装包下载 版本号:7.4 下载 提取码:fxma 2 安装方法 用安装包内的Key激活即可.软件激活后不能升级. 注意:必须断网或者加入防火墙阻止联网使用! 3 可能遇到的问题 当安装完Math ...

  2. elasticsearch river 参数文档

    JDBC River parameters Jörg Prante edited this page on 23 Jan 2014 · 3 revisions Pages 15 Home Bulk i ...

  3. sql语句怎么看效率?

    1.数据库设计当面: 对查询进行优化,应该尽量避免全表扫描,首先应考虑在where及order by设计的列上加索引. d.索引并不是越多越好,索引可以提高查询效率,同时降低了insert和updat ...

  4. 洛谷P3749 [六省联考2017]寿司餐厅

    传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...

  5. Joomla 随记

    使用 joomla 开发好几个网站了,记录一下基础的东西吧~ 一.下载: 进入 joomla官网 下载:   二.安装 Joomla: 把下载好的 joomla 放到网站文件夹(图为wamp阿帕奇服务 ...

  6. crontab定时任务语法及应用

    https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows ...

  7. Python - 基本数据类型及其常用的方法之列表

    列表: 特点:用 [] 括起来,切元素用逗号分隔:列表内的元素可以为任何的数据类型. 列表的基本操作: 1.修改 li = [12, 5, 6, ["Aiden", [2, 4], ...

  8. RandomRowFilter(3)

    比较容易理解 用来随机抽取 RandomRowFilter:从名字上就可以看出其大概的用法,本过滤器的作用就是按照一定的几率(<=0会过滤掉所有的行,>=1会包含所有的行)来返回随机的结果 ...

  9. Python中函数的定义必须在调用的前面

    # -*- coding:utf-8 -*- Python中函数的定义必须在调用的前面,但是在函数的内部调用一个函数,不用考虑顺序,只要被调用的函数被定义了即可 #标准的先函数定义,后函数调用def ...

  10. 关于Git回退再前进造成本地代码和远程仓库代码不一致的问题

    事情经过:  git push 提交之后(版本2.0), 回退, 然后做了一些修改, 发现有问题,于是脑抽回退git reset --hard HEAD^ (版本1,0), 然后又前进到之前那个版本( ...