一年没有管理博客园了,说来实在惭愧。。

最近开始刷LeetCode,之前没刷过,说来也实在惭愧。。。

刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看心情随便选题…(⊙o⊙)…今天做了14年 Add 的三个题,其中 Maximum Product Subarray 着实把我折腾了好一会儿,所以想想还是跟大家分享一下我的解法,也或许有更好的方法,期待大家的分享。就把三个题按 Add Date 的先后顺序分享一下吧。

Add Date 2014-03-05

Reverse Words in a String

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

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

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.

单纯把 "the sky is blue" reverse 成 "blue is sky the" 是件很容易的事情,也是比较经典的题目,不过本题说:

1. s 中的开头和结尾有可能有空格,reverse 后的 string 中要去掉;

2. 连续多个空格要变成一个空格。

我的 code 略折腾,用了两个函数:

1. reverseWord 实现最基本的 reverse,首先整个 reverse,变为“eulb si yks eht”,然后每个 word reverse,遍历两边即可。

2. removeSpace 实现检查和删除空格。遍历一遍即可。

复杂度O(n).

附 code,仅供参考。

 class Solution {
public:
void reverseWord(string &s) { //反转字符串
string::iterator pre = s.begin();
string::iterator post = s.end()-;
char tmp;
while(pre < post) { //反转整个字符串
tmp = *pre;
*pre = *post;
*post = tmp;
++pre;
--post;
}
pre = s.begin();
post = pre;
while(post != s.end()) { //反转每个单词
while(pre != s.end() && *pre == ' ') {
++pre;
}
if(pre == s.end())
return;
post = pre;
while(post != s.end() && *post != ' ') {
++post;
}
--post;
if(post != s.end() && pre < post) {
string::iterator p1 = pre;
string::iterator p2 = post;
while(p1 < p2) {
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
++p1;
--p2;
}
}
++post;
pre = post;
}
} void removeSpace(string &s) { //检查和删除空格
string::iterator pre = s.begin();
string::iterator post = pre;
while(pre != s.end() && *pre == ' ') { //删除开头的空格
s.erase(s.begin());
pre = s.begin();
}
if(pre == s.end())
return;
post = pre;
while(post != s.end()) { //把连续多个空格变为一个
while(pre != s.end() && *pre != ' ')
++pre;
if(pre == s.end())
return;
post = pre+;
while(post != s.end() && *post == ' ') {
s.erase(post);
post = pre+;
}
++pre;
}
if(!s.empty()) { //如果最后有空格则删除
pre = s.end()-;
if(*pre == ' ')
s.erase(pre);
}
} void reverseWords(string &s) {
reverseWord(s);
removeSpace(s);
}
};

【LeetCode】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——Reverse Words in a String 旋转字符串中单词顺序(AC)

    题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...

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

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

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

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

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

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

  7. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  8. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III

    公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...

  9. [Swift]LeetCode557. 反转字符串中的单词 III | 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 ...

随机推荐

  1. 关于PM的认识

    1 我眼中的PM 1.1 人云“一个管理,半个专家”,我说“一个管理,两个专家” 如今,我发现我们不得不面对这样一个现实——角色兼职.我习惯上把项目分为三类:性命攸关的项目(涉及到人身安全的项目,如铁 ...

  2. HBase——完全分布

    实际上,在真实环境中你需要使用完全分布配置完整测试HBase.在一个分布式配置中,集群有多个节点,每个节点运行一个或多个HBase守护进程.其中包括主Master和备份Master实例,多个Zooke ...

  3. Java多线程下载文件

    package com.test.download;   import java.io.File; import java.io.InputStream; import java.io.RandomA ...

  4. PHP通过prepare执行查询取得数据

    可以用来防止sql注入 <?php $pdo=new PDO("mysql:host=localhost;dbname=itest", 'root',''); //先构建查询 ...

  5. android 5.0 默认水波纹背景属性,可设置不论什么View

    actionBarItemBackground   5.0以上超出边界圆形水波纹 selectableItemBackground  5.0以上边界内圆形水波纹 这两个属性在5.0下面是默认的灰色效果 ...

  6. 完好用户体验: 活用window.location与window.open解决页面跳转问题

    原文地址: JavaScript Redirects and window.open原文日期: 2014年08月27日翻译日期: 2014年08月31日翻译人员: 铁锚 (译者注: 本文解决的是按 C ...

  7. XP,32/64位Win7,32/64位Win8,32/64位Win10系统 【春节版】

    本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...

  8. 2015年多校联合训练第一场OO’s Sequence(hdu5288)

    题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个相应的j使得a[i]%a[j]=0.那么l,r内有多少个i,f(l,r)就是几. 问全部f(l,r)的 ...

  9. python 基础及资料汇总

    Python 包.模块.类以及代码文件和目录的一种管理方案     Numpy 小结   用 Python 3 的 async / await 做异步编程  K-means 在 Python 中的实现 ...

  10. SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

    LCS2 - Longest Common Substring II no tags  A string is finite sequence of characters over a non-emp ...