LeetCode 5:Given an input string, reverse the string word by word.
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.的更多相关文章
- 【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 ...
- reverse the string word by word
题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...
- [string]Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [C/C++] String Reverse 字符串 反转
#include <iostream> #include <string> #include <algorithm> #include <cstring> ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 该死的类型转换For input string: "[Ljava.lang.String;@1352dda"
今天又遇见了这个该死的问题,还是记下来备忘. 从map里取值的时候,将OBJECT对象 先转换成String 然后转换成integer报错 java.lang.NumberFormatExceptio ...
- 如何将List<string>转化为string
Convert List, string. A List can be converted to a string. This is possible with the ToArray method ...
- 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 ...
- leetcode@ [79/140] Trie树应用 Word Search / Word Search II
https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...
随机推荐
- python 基础篇 10 函数进阶
本节主要内容:1. 函数参数--动态传参2. 名称空间, 局部名称空间, 全局名称空间, 作⽤域, 加载顺序.3. 函数的嵌套4. gloabal, nonlocal关键字 ⼀. 函数参数--动态传参 ...
- LightGBM详细用法--机器学习算法--周振洋
LightGBM算法总结 2018年08月21日 18:39:47 Ghost_Hzp 阅读数:2360 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...
- 学习bash——数据流重定向
一.概述 1. 数据流 定义:以规定顺序被读取一次的数据序列. 分类:标准输入(stdin).标准输出(stdout)和标准错误输出(stderr). 标准输出:指的是命令执行所回传的正确信息. 标准 ...
- postman工具中如何提取接口的返回值
提取接口返回值 当返回值是返回JSON时 1.let json = JSON.parse(responseBody); // responseBody是包含整个返回内容的字符串 提取某字段的值: ...
- java线程(5)——线程池(上)
引入: 在之前的例子中,我们需要使用线程时就直接去创建一个线程,这样既不浪费资源又十分方便.但如果我们需要创建多个并发的线程,而且短时间执行就结束了,如果还用之前的方式,就会大大降低效率和性能了. 因 ...
- 【Android入门】——模拟器的创建及常见问题汇总
[前言] 刚刚接触Android,第一门课我们就来创建一个模拟器.安卓模拟器,简称AVD(Android Virtual Device),是安卓运行的虚拟设备.有了他以后,我们就不需要在连着安卓手机进 ...
- 【历史】- .NET之父 - Anders Hejlsberg
简介 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi和.NET之父! 安德斯·海尔斯伯格曾在丹麦技术大学学习工 ...
- Linux上删除空行的方法
grep . data.txt grep-v'^$' data.txt grep'[^$]' data.txt sed'/^$/d' data.txt sed'/^\s*$/d' data.txt # ...
- (转)Loadrunner监控Linux的17个指标
1.Average load:Average number of processes simultaneously in Ready state during the last minute. 上 ...
- 在C的头文件中定义的结构体,如何在cpp文件中引用
解决方案1:在cpp文件中放置.c,且在该文件中引用变量 解决方案2:在一个cpp文件中包含.c,但在另一个cpp文件中使用结构体变量 cpp文件1 cpp文件2 #include "dia ...