题目:将一个英文句子翻转,比如:the sky is blue 翻转后变为:blue is sky the

分析:我的实现方法是,利用栈将单词存储起来,然后再顺序拿出来,单词进栈还需注意添加空格。

主要代码:

class Solution {
public:
	void reverseWords(string &s) {
		stack<string> mStack;
		string mString = "";
		bool flag = false;//false 表示遇到空格,true表示正在读一个单词
		int mStringLength = s.length();
		for (int i=0;i<mStringLength;++i)
		{
			//获取一个单词
			if (s[i] != ' ')
			{
				flag = true;
				mString += s[i];
			}
			//将一个单词入栈
			if ((s[i] == ' ' || i == mStringLength-1) && flag == true)
			{
                mStack.push(mString);
				flag = false;
				mString = "";
				mStack.push(" ");
			}
		}
		mStack.pop();//将最后一个空格丢掉
		//将s中的单词反置
		s.clear();
		while(!mStack.empty())
		{
			s.append(mStack.top());
			//s += mStack.top();
			mStack.pop();
		}
	}
};

  

LeetCode之ReverseWorldString的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Android studio教程:[1] 创建app项目

    Android studio作为面市不久的安卓开发工具,越来越受到大家的喜爱,这里我将介绍如何在Android studio中创建一个APP项目,并在以后经验中介绍其他有关Android studio ...

  2. dojo.hasClass/dojo.addClass/dojo.removeClass/dojo.toggleClass/dojo.repalceClass

    dojo.hasClass(/*DomNode*/node or DomIdstring,/*String*/classString)//如果节点中有特定的类,那么返回ture,否则返回false d ...

  3. linux下C++对线程的封装

    之前一直是使用C语言,前段时间转做C++.无论使用什么语言,多线程编程都是不可或缺的.最近项目中又用到了线程,现在将线程的封装做出总结: 1.线程类中应该包含线程ID.线程的状态以及线程基本操作等. ...

  4. 龙芯3A上V8的编译与测试

    使用平台: loongson3a+debian6.0.3+linux2.6.36.3+gcc4.6.3 一: V8的下载 这里V8是从其官网上使用git下载的: (1)如果没有git和git-svn需 ...

  5. QTestLib Tutorial

    本学习指南介绍了如何使用QTestLib框架的一些特性,分为4章: 编写一个单元测试程序 数据驱动测试 模拟GUI事件 重复GUI事件 第一章 编写一个单元测试程序 文件列表: qtestlib/tu ...

  6. C++标准程序库读书笔记-第二章新的语言特性

    1.基本类型的显式初始化 如果采用不含参数.明确的constructor(构造函数)调用语法,基本型别会被初始化为零: int i1; //undefined value int i2 = int() ...

  7. thinkphp框架开启页面gzip压缩

    Thinkphp下开启gzip压缩很简单,不管你是哪个版本,只要在你的入口文件index.PHP中加入以下两行,如果你的服务器支持,那么就OK了. define ( "GZIP_ENABLE ...

  8. Change the ball--hdu2277

    Change the ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. HDU1043 Eight(BFS)

    Eight(South Central USA 1998) Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  10. UVA 12563 Jin Ge Jin Qu hao

    dp-背包 开始用普通dp写了一发发现没法确定最大时间... 后来看到大牛机智的写法,嗯...dp表示当前状态能否成立:然后从条件最好的状态开始遍历,直到这个状态成立然后退出遍历. 具体的看代码吧.. ...