LeetCode 151 reverse word 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".
Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space.
这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2)标点符号reverse?
主要思路:(1)万事开头难,你必须首先就把一些空串,单个字符串等一些在后面会出现bug的输入处理掉!!!
(2)把第一步处理好之后,将整个字符串翻转过来,标点当成字符处理。
(3)每个单词翻转,并用一个string存储。然后补充一个空格,直到处理完整个串。
(4)判断字符串是否为空(如果前面输入多个空格,到这里得到一个空字符串),非空则删除最后一个空格。
(5)输出结果。
在单词翻转的时候,使用布尔型wordHead记录是否是单词头而不是空格,确保只针对单词翻转。
class Solution {
public:
void reverseWords(string &s)
{
int n = s.size() - ;
int wHead = ;
int wTail = n ;
char cPunct = ' ';
if(s.size() == )
{
return;
}
if( n == )
{
if(s[] == ' ')
{
s.clear();
}
return;
}
while( wHead < wTail )
{
char temp = s[wTail];
s[wTail] = s[wHead];
s[wHead] = temp;
wHead++;
wTail--;
}
wTail = wHead = ;
string sRet;
bool wordHead = true;
while(wTail <= n)
{
if(!isspace(s[wTail]) && wordHead)
{
wHead = wTail;
wordHead = false;
}
if( ( s[wTail] == ' ' || wTail == n) && !isspace(s[wHead]))
{
if(wTail == n && !isspace(s[wTail]))
wTail++;
sRet += SwapWord(s, wHead, wTail - ) + " ";
wHead = wTail + ;
wordHead = true;
}
wTail++;
}
s = sRet;
if(s.size() > )
s.erase(s.size()-, );
}
string SwapWord(string &s, int wHead, int wTail)
{
int wStart = wHead;
int wEnd = wTail;
while( wStart < wEnd )
{
char temp = s[wStart];
s[wStart] = s[wEnd];
s[wEnd] = temp;
wStart++;
wEnd--;
}
return s.substr(wHead, wTail - wHead + );
}
};
应该认真的对待OJ上自己力所能及的每一题,多思考!
LeetCode 151 reverse word in a string的更多相关文章
- [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& ...
- Java for 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 ...
- leetcode 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- Leetcode#151 Reverse Words in a String
原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...
- [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
随机推荐
- 怎么在windows10中关闭Windows Defender?
通过修改注册表,永久禁用Windows Defender 打开注册表编辑器. 按 Win +R键入regedit,点击确定. 定位需要修改的注册表 其路径如下 HKEY_LOCAL_MACHIN ...
- 分布式文件系统---GlusterF
1.1 分布式文件系统 1.1.1 什么是分布式文件系统 相对于本机端的文件系统而言,分布式文件系统(英语:Distributed file system, DFS),或是网络文件系统(英语:Ne ...
- 教你如何用Docker快速搭建深度学习环境
本教程搭建集 Tensorflow.Keras.Coffe.PyTorch 等深度学习框架于一身的环境,及jupyter. 本教程使用nvidia-docker启动实例,通过本教程可以从一个全新的Ub ...
- c++知识点总结--函数模板
通用函数可变参模板 用于处理不限定参数的函数 showall(){//空函数,接口,最后结束递归 } template<typename T,typename... Args> void ...
- NO5——素数筛选
#include <stdio.h> int main() { ]={}; ;i<=;i++) a[i]=; ;i<=;i++) if(a[i]) ;j+=i) a[j]=; ...
- 揭开网络编程常见API的面纱【上】
Linux网络编程API函数初步剖析 今天我们来分析一下前几篇博文中提到的网络编程中几个核心的API,探究一下当我们调用每个API时,内核中具体做了哪些准备和初始化工作. 1.socket(famil ...
- lintcode-74-第一个错误的代码版本
74-第一个错误的代码版本 代码库的版本号是从 1 到 n 的整数.某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错.请找出第一个错误的版本号. 你可以通过 isBad ...
- Tomcat 中如何给 web 项目配置虚拟目录的方法
为什么要给 web 项目配置虚拟目录? 初学 JavaWeb 时,会发现只要我们把 web 项目放到 Tomcat 的 webapps 目录下,再通过 http://localhost:8080/项目 ...
- exit和die的区别
网上搜索die与exit两个函数的区别,大部分的"标准答案"都是说die是退出并释放内存,exit是退出但不释放内存. 这个解释显然是错的,PHP手册中已经说过"die ...
- arcgis的炸开多边形功能
有时候我们使用dissolve工具,或其他操作会将空间不相连的多边形对应的属性合并到一起,如图: 在高级编辑工具中: 有这样一个工具,但是它能满足我的要求,但是他不是批量的,不过它使用起来比较方便. ...