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. 孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6

    孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 不过由于对python-docx模 ...

  2. 问题 D: 约数的个数

    问题 D: 约数的个数 时间限制: 1 Sec  内存限制: 32 MB提交: 272  解决: 90[提交][状态][讨论版][命题人:外部导入] 题目描述 输入n个整数,依次输出每个数的约数的个数 ...

  3. deeplearning.ai课程学习(4)

    第四周:深层神经网络(Deep Neural Networks) 1.深层神经网络(Deep L-layer neural network) 在打算使用深层神经网络之前,先去尝试逻辑回归,尝试一层然后 ...

  4. N-grams模型、停顿词(stopwords)和标准化处理 - NLP学习(2)

    在上一节<Tokenization - NLP(1)>的学习中,我们主要学习了如何将一串字符串分割成单独的字符,并且形成一个词汇集(vocabulary),之后我们将形成的词汇集合转换成计 ...

  5. MVC项目用Windsor注入

    第一步创建controler 注入类 public class ApiControllersInstaller : IWindsorInstaller {  public void Install(I ...

  6. PyQt5图像全屏显示

    Windows装这个:https://pypi.python.org/pypi/PyQt5Ubuntu输入这个:sudo apt-get install python3-pyqt5 或者直接输入:pi ...

  7. MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails

    具体报错信息: Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `t ...

  8. 【iOS开发】多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    http://blog.csdn.net/crycheng/article/details/21799611 本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSI ...

  9. 学习MVC中出现的一个BUG

    BUG描述:No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.S ...

  10. [转] const int *a与int *const a,const int *const a的区别

    http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...