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. 问题 C: Goldbach's Conjecture

    题目描述 Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least ...

  2. 九度OJ--Q1164

    import java.util.Scanner; /* * 题目描述: * 任意输入两个9阶以下矩阵,要求判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0.90.180.270),如果不是 ...

  3. 深度学习anchor的理解

    摘抄与某乎 anchor 让网络学习到的是一种推断的能力.网络不会认为它拿到的这一小块 feature map 具有七十二变的能力,能同时从 9 种不同的 anchor 区域得到.拥有 anchor ...

  4. Dijkstra模板 dj斯特拉

    图论里非常常用的dijkstra,自己加了个路径查找,做个模板吧: ; struct Edge { int from,to,dist; Edge(int u, int v, int d):from(u ...

  5. STL应用——hdu1702(队列+堆栈)

    水题 练习一下堆栈和队列的使用 #include <iostream> #include <cstdio> #include <algorithm> #includ ...

  6. C# 几种读取MAC地址的方法

    以下是收集的几种C#程序读取MAC地址的方法,示例中是读取所有网卡的MAC地址,如果仅需要读取其中一个,稍作修改即可. 1 通过IPConfig命令读取MAC地址 ///<summary> ...

  7. jquery $.getJSON 注意细节

    服务端: var json = "{\"title\": \"Recent Uploads tagged mountrainier\",\" ...

  8. [C/C++] static在C和C++中的用法和区别

    转自:http://blog.csdn.NET/skyereeee/article/details/8000512 static的三个作用: (1)局部静态变量 (2)外部静态变量/函数 (3)静态数 ...

  9. [剑指Offer] 15.反转链表

    题目描述 输入一个链表,反转链表后,输出链表的所有元素. [思路1]三个指针在链表上同时滑动. /* struct ListNode { int val; struct ListNode *next; ...

  10. spring中context:property-placeholder

    发现网上对于这个标签的解释过于复杂,这里从实用性角度简短的进行说明. 首先,它是spring3中提供的标签. 只需要在spring的配置文件里添加一句: <context:property-pl ...