problem:

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

For example:

Given s = "the sky is blue",

return "blue is sky the".

问题分析:如何准确的找到每一个需要清除的空格的位置pos,以及每个word对应的pos范围?

解决方法:需要反转一个字符串,使用int string.find_last_not_of(char c, int pos=npos);该函数是从字符串最后往前搜索最后一个不是字符c的位置.

在使用int string.find_first_of(char c, int pos=npos);该函数是从pos从后往前搜索第一个c出现的位置,并且返回。

还有要说明的是string& string.assign(const string &s, int start, int n);该函数是将s中从start出开始的n个字符赋给当前字符串。

基于上面的说明代码如下:

class Solution
{
public:
void reverseWords(string &s)
{
string strTmp;
string Dst; size_t nLastWord = s.find_last_not_of(" ");
size_t nLastSpace = string::npos; while( nLastWord != string::npos )
{
nLastSpace = s.find_last_of(" ", nLastWord);
if (nLastSpace == string::npos)
{
strTmp.assign(s, , nLastWord + );
Dst += strTmp; break;
}
else
{
strTmp.assign(s, nLastSpace + , nLastWord - nLastSpace);
Dst += strTmp;
} nLastWord = s.find_last_not_of(" ", nLastSpace);
if (nLastWord == string::npos)
{
continue;
} Dst += " ";
} s = Dst;
}
};

感谢那些无偿贴出自己程序的供我学习的大神。
总结:基础知识很不扎实,还需要深入学习掌握STL及核心的数据结构,数据排序,查找算法。

LeetCode 5:Given an input string, reverse the string word by word.的更多相关文章

  1. 【LeetCode刷题Java版】Reverse Words in a String

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

  2. reverse the string word by word

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

  3. [string]Reverse Words in a String

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

  4. [C/C++] String Reverse 字符串 反转

    #include <iostream> #include <string> #include <algorithm> #include <cstring> ...

  5. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

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

  6. 该死的类型转换For input string: "[Ljava.lang.String;@1352dda"

    今天又遇见了这个该死的问题,还是记下来备忘. 从map里取值的时候,将OBJECT对象 先转换成String 然后转换成integer报错 java.lang.NumberFormatExceptio ...

  7. 如何将List<string>转化为string

    Convert List, string. A List can be converted to a string. This is possible with the ToArray method ...

  8. leetcode@ [139/140] Word Break & Word Break II

    https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, determine ...

  9. leetcode@ [79/140] Trie树应用 Word Search / Word Search II

    https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...

随机推荐

  1. 「Haskell 学习」二 类型和函数(上)

    随着学习的深入,笔记会补充和修订.当然,这个补充修订也许会鸽,但我一定会坚持写完. 这个笔记假定你至少学过C/C++及Python,或与这两种语言类型相同的语言. 类型系统概述 “Haskell’s ...

  2. Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建

    相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.4.4-nts-Win32-VC9-x86.zip 如果是win2 ...

  3. vs code 在终端下使用 code ./ 打开当前项目

    Mac OS Visual Studio Code的扩展工具菜单中有Install command line的快捷安装 运行 VS code并打开命令面板( ⇧⌘P ),然后输入 shell comm ...

  4. LeetCode 74——搜索二维矩阵

    1. 题目 2. 解答 若矩阵为空,比如 [], [[]],此时直接返回 false. 若目标值小于矩阵第一个元素或者大于矩阵最后一个元素,则目标值不在矩阵范围内,直接返回 false. 其他情况下, ...

  5. js中迭代元素特性与DOM中的DocumentFragment类型 笔记

    JS中迭代元素特性 在需要将DOM结构序列化为XML或者HTML字符串时,多数都会涉及遍历元素的特性,这个时候attributes属性就可以派上用场. 以下代码展示了如何迭代元素的每一个特性,然后将他 ...

  6. lintcode-84-落单的数 III

    84-落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...

  7. WCF 动态调用(动态创建实例接口)

    很多时候,服务地址都不止一个的,这个时候就要动态去配置地址.配置Web.config,很麻烦 下面就看看怎样实现动态调用WCF. 首先看看动态创建服务对象的代码: using System; usin ...

  8. hibernate笔记(二)

    目标: 关联映射(hibernate映射) 1. 集合映射 2. 一对多与多对一映射 (重点) 3. 多对多映射 4. inverse/lazy/cascade 1. 集合映射 开发流程: 需求分析/ ...

  9. 【bzoj4724】[POI2017]Podzielno 二分

    题目描述 B进制数,每个数字i(i=0,1,...,B-1)有a[i]个.你要用这些数字组成一个最大的B进制数X(不能有前导零,不需要用完所有数字),使得X是B-1的倍数.q次询问,每次询问X在B进制 ...

  10. Word2010 自动生成二级编号

    http://jingyan.baidu.com/article/3ea5148901919752e61bbafe.html