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的更多相关文章

  1. [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& ...

  2. 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 ...

  3. 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& ...

  4. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

  5. Leetcode#151 Reverse Words in a String

    原题地址 将单词按空格分词,然后倒序拼接即可 代码: void reverseWords(string &s) { vector<string> words; ; ; ; i &l ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

随机推荐

  1. Windows模拟linux终端工具Cmder+Gow

    1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...

  2. tp5.0 模型查询数据的返回类型,分页

    一开始用painate()这个函数的时候,发现有的查询方式不能使用这个函数,由此了解到了模型查询和普通查询返回类型的不同 1.原生查询方法 Db::query("select * from ...

  3. [Effective Java] 创建和销毁对象篇

    [Effective Java] 创建和销毁对象篇 1. 优先考虑用静态工厂方法代替构造器 优点: - 静态工厂方法相比于构造器,它们有名称 - 不需要每次在使用的时候创建一个对象 - 可以返回原返回 ...

  4. python之*args和**kwargs参数,以及迭代器

    *args让函数可以接受不限制多个位置参数,**kwargs让函数可以接受不限制多个关键字参数,用法如图 2.迭代器总结

  5. java面笔准备

    这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的朋友在笔试时更好地赢得笔试和面试.由于这套面试题涉及的范围很泛,很广,很杂,大家不可能一天两天就看完 ...

  6. hbase1.2.6完全分布式安装

    环境,参考之前的两篇博文: jdk1.7 hadoop2.6.0 完全分布式 一个master,slave1,slave2,slave3 zookeeper3.4.6 完全分布式 安装与配置:(以下步 ...

  7. PAT 1030 完美数列

    https://pintia.cn/problem-sets/994805260223102976/problems/994805291311284224 给定一个正整数数列,和正整数 p,设这个数列 ...

  8. JVM(2)——GC算法和收集器

    一.引入 上篇博客<JVM--简介>中主要介绍了JVM的内存模型,思考一下: 为什么要划分堆.栈.方法区等? 为什么把不同种类的数据信息分别存放? 答案可以分为很多很多条,这里就说一个方面 ...

  9. 从pthread 到QThread

    该文出自:http://www.civilnet.cn/bbs/topicno/78430 使用线程技术进行应用编程的意义在gemfield的文章<从进程到线程>中已经介绍过了,我们就直奔 ...

  10. wait_event_interruptible_timeout

    最近一套方案涉及到内核线程之间的同步,用到了函数wait_event_interruptible_timeout函数,大致是这样: A:是一个后台的线程,平常没事就睡觉,有时被唤醒,或者每5分钟醒一次 ...