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 ...
随机推荐
- Android4.0系统以上程序不出现菜单键的问题解决
去掉targetSdkVersion 或改为targetSdkVersion =13或更小.. 不改targetSdkVersion的办法:在onCreate() 里setContentView()之 ...
- 关于Vue脚手架写法的问题
问题描述: main.js import Vue from 'vue' import App from './App' /* eslint-disable no-new */ new Vue({ el ...
- 今日Linux下安装部署禅道
我的linux系统是在虚拟机上安装的Ubuntu,禅道在官网www.zentao.net下载安装的开源版的linux64位,采用一键安装包安装.安装前要求:系统上不能有自己安装的mysql .下载的安 ...
- CodeForces-1132C Painting the Fence
题目链接 https://vjudge.net/problem/CodeForces-1132C 题面 Description You have a long fence which consists ...
- 学习人工智能的第六个月[深度学习[Deep Learning,DL]]
这个月阅读了论文[Partial Adversarial Domain Adaptation-eccv18],文章着眼于源域标签空间包含目标域标签空间的场景,在域对抗神经网络的基础上提出了部分对抗域适 ...
- web开发速查表(php,css,html5........)
- linux tcpdump抓包,wireshark实时解析
转自: http://www.freebuf.com/articles/wireless/6517.html 由于CentOS7上yum安装的wireshark对CoAP的解析支持不太完善,而我w ...
- delphi中写SQL语句中变量的注意事项
1.procedure TForm1.btn1Click(Sender: TObject); var s: String; begin S := 'select * from TMarketI ...
- hibernate笔记(二)
目标: 关联映射(hibernate映射) 1. 集合映射 2. 一对多与多对一映射 (重点) 3. 多对多映射 4. inverse/lazy/cascade 1. 集合映射 开发流程: 需求分析/ ...
- Impala-1
Impala相关操作上 阅读目录 序 数据库相关 表相关 系列索引 序 上一篇,我们介绍Impala的介绍及安装. 下面我们开始继续进一步的了解Impala的相关操作. 数据库相关 一:创建 ...