题目:将一个英文句子翻转,比如: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. 使用FMDB,libqrencode实现二维码的生成并且保存到数据库

    /**  * 1.首先导入第三方库FMDB和libqrencode,添加库libsqlite3.tbd     

  2. hdu2317Nasty Hacks

    Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...

  3. MYSQL5.6数据库ZIP安装以及VS中使用注意事项

    先挂资源,下载地址: http://download.softagency.net/MySQL/Downloads/. 找到MYSQL5.6,两个版本看系统而定我下的是64位的:http://down ...

  4. Fragment之三:根据屏幕尺寸加载不同的Fragment

    Fragment一个重要的作用在于根据屏幕的尺寸或者方向加载不同的布局. 未完待续

  5. Android 使用BroadcastReceiver的几种方法

    发送自定义广播 全局广播 发送标准广播 1.定义广播接收器.(在发送广播前,需要先定义一个广播接收器,不然发了也是白发) public class MyBroadcastReceiver extend ...

  6. MongoDB安装说明

    1.去官网(https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-3.2.3-signed.msi/down ...

  7. 正式学习React(一) 开始学习之前必读

    为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...

  8. Firebug Command Line

    http://michaelsync.net/2007/09/15/firebug-tutorial-commandline-api

  9. Amzon MWS API开发之 上传数据

    亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...

  10. U盘开发之SSD对比

    U盘因其小巧方便,逐步取代了笨重的移动硬盘和光驱,成为最普及的存储介质.现在的主板BIOS也将支持USB启动,作为标准之一,再过几年,光驱时代可能就要终结了.从早期的16MU盘,到现在动辄几个G,U盘 ...