题目:将一个英文句子翻转,比如: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. 76 bytes for faster jQuery

    转载自http://james.padolsey.com/javascript/76-bytes-for-faster-jquery/ 作者JAMES PADOLSEY 在我们平时使用JQuery,调 ...

  2. 表设计与SQL优化

    1. 说说分区表的主要好处是什么,为什么会有这些好处. 分区功能能够将表.索引或索引组织表进一步细分为段,这些数据库对象的段叫做分区.每个分区有自己的名称,还可以选择自己的存储特性. 从数据库管理员的 ...

  3. hibernate总结一

    在hibernate中查询使用List,Map和类对象定制返回类型   在使用hibernate进行查询时,使用得最多的还是通过构建hql进行查询了.在查询的过程当中,除使用经常的查询对象方法之外,还 ...

  4. stuts1:(Struts)Action类及其相关类

    org.apache.struts.action.Action类是Struts的心脏,也是客户请求和业务操作间的桥梁.每个Action类通常设计为代替客户完成某种操作.一旦正确的Action实例确定, ...

  5. 网易云数据结构- Maximum Subsequence Sum

    题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...

  6. Eclipse运行Tomcat7源码

    1. 各环境版本: jdk1.6.0_45 (亲测jdk1.7.0_07会有问题,不要用1.7版本的) apache-ant-1.9.4 apache-tomcat-7.0.61-src 2. 安装a ...

  7. Oracle表分区[转]

    废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...

  8. MYSQL select ....outfile.....from.....

    select .... outfile  'file_path' fields terminate by '\t' lines terminate by '\r\n' from table_name; ...

  9. Android 控件属性

    TextView 文字属性//文字左右居中android:layout_centerHorizontal="true"//文字垂直居中android:layout_centerVe ...

  10. Inno Setup技巧[界面]自定义安装向导小图片宽度

    原文  blog.sina.com.cn/s/blog_5e3cc2f30100cj7e.html 英文版中安装向导右上角小图片的大小为55×55,汉化版中为55×51.如果图片超过规定的宽度将会被压 ...