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 ...
随机推荐
- Mootools 学习随笔
简单的介绍下Mootools: MooTools是一个简洁,模块化,面向对象的开源JavaScript web应用框架.在处理js.css.html时候,为web开发者提供了一个跨浏览器的js解决方案 ...
- python 自动化-"Elements not visible"
一,今天试着跑一个多乘客下单的python脚本, 总是遇到 Elements not visible 或者 not clickable的错误 解决方法: 1. 首先观察脚本运行时, 报错的那个元素 ...
- 简历编写技巧-java开发工程师简历实战
看到一遍简历编写的文章 想到也快找工作了 早晚能够用上 现在摘录如下 640?wx_fmt=jpeg 工欲善其事,必先利其器,这是自古以来的道理.所以如果想找到一份好的工作,一定要先整理一份好的简历. ...
- phpcms v9手机门户配置方法
一.确定一个域名作为你手机wap站点的访问域名,例如:http://m.tezhengzong.com. 接下来在域名管理系统中简析这个域名到你的服务器地址. 二.修改\caches\configs\ ...
- android多点触控自由对图片缩放
在系统的相册中,观看相片就可以用多个手指进行缩放. 要实现这个功能,只需要这几步: 1.新建项目,在项目中新建一个ZoomImage.java public class ZoomImageView e ...
- C#数据库连接问题
最近在看C#,今天下午刚开始接触C#的数据库连接,SQL Server2008,问题如图:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名 ...
- Linux在线用户通信
1,/etc/motd文件 该文件即 message of today(布告栏信息),每次用户登录时,/etc/motd文件的内容会显示在用户的终端.系统管理员可以在文件中编辑系统活动消息 即像公告栏 ...
- Struts1之logic标签
logic是Struts1中的逻辑标签 <%@ taglib prefix="logic" uri="http://struts.apache.org/tags-l ...
- 【题解】Bzoj4316小C的独立集
决定要开始学习圆方树 & 仙人掌相关姿势.加油~~ 其实感觉仙人掌本质上还是一棵树,长得也还挺优美的.很多的想法都可以往树的方面上靠,再针对仙人掌的特性做出改进.这题首先如果是在树上的话那么实 ...
- [POI2006] OKR-period of words
传送门 - > \(bzoj 1511\) 题目描述 A string is a finite sequence of lower-case (non-capital) letters of t ...