题目:将一个英文句子翻转,比如: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. XML.ObjTree -- XML source code from/to JavaScript object like E4X

    转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...

  2. 解决net-snmp正确输出MAC地址和判断空的IP地址

    function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence ( ...

  3. 工厂方法配置jdbc连接

    package 配置方式.dbUtils; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver ...

  4. theano安装

    theano安装有两类方法,一种是自己一步步安装,还有一种是借助其他软件安装.我是安装到一半发现第二种方法的...........所以就用的第一种麻烦的办法装的,但是过程也是一种学习. 电脑:win7 ...

  5. cocos2dx工程中接入支付宝sdk

    1. 首先去支付宝官网下载开发者文档 2. 然后按着开发者文档将支付宝的sdk导入到你的工程中,并关联到工程中,步骤入下图: (1)将从支付宝官方网站获得的支付宝的sdk的jar包拷贝到工程中的lib ...

  6. 正式学习React( 三)

    最基本的jsx语法什么的,我就不介绍了,唯一觉得有用点的,就是声明周期了. 下面的内容是转来的,自己也可以网上去搜,我觉得别人归纳的挺不错的,不过写法可能不是es6的,不影响学习. 在组件的整个生命周 ...

  7. MVC 后台DataTable 前台遍历

    /// <summary> /// 多级审批流展示 /// </summary> /// <returns></returns> public Acti ...

  8. 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析

    ViewPager中切换界面Fragment被销毁的问题分析  原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...

  9. logstash grok正则调试

    logstash 正则调试: nginx 配置: log_format main '$remote_addr [$time_local] "$request" '; logstas ...

  10. js混淆 反混淆 在线

    js反混淆地址:http://www.bm8.com.cn/jsConfusion/ 在线javascript 混淆http://www.moralsoft.com/jso-online/hdojso ...