LeetCode之ReverseWorldString
题目:将一个英文句子翻转,比如: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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- ng-class用法
在angular中为我们提供了3种方案处理class: 1:scope变量绑定.这种方案不推荐,因为scope里最好处理业务逻辑,不去管渲染的事.2:字符串数组形式.3:对象key/value处理. ...
- Unity IOC注入详细配置(MVC,WebApi)
一直想写一篇关于unity 详细的配置信息的文章,也算是自我总结吧 先介绍了unity , Unity是微软官方推荐使用的轻型的IOC框架,支持各种方式的注入 ,使用来解耦的利器. 获取unity 的 ...
- C#中大List的内存分配
之前在开发中只用到List的时候几乎就是拿过来就用,从来没有考虑过List的内存分配问题,试想一个有10万元素的List的在构造和添加元素时内存是如何变化的呢?在MSDN上关于List的Capacit ...
- C#之回到了最初的起点----解决方案、项目、程序集、命名空间
C#之回到了最初的起点----解决方案.项目.程序集.命名空间 ——Percy 初学者很容易把这些概念搞混淆.先说说项目(Project),通俗的说,一个项目可以就是你开发的一个软件.在.Net下,一 ...
- commands - `for`
internal variable of separator: IFS
- Linux下MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法
本文为转载内容,感谢原作者.原文出自:http://zhaoxiaoru39.blog.163.com/blog/static/609552192012511104730115/ 我遇到的问题是:在n ...
- AJAX防重复提交的办法总结
最近的维护公司的一个代理商平台的时候,客服人员一直反映说的统计信息的时候有重复数据,平台一直都很正常,这个功能是最近新进的一个实习生同事写的功能,然后就排查问题人所在,发现新的这个模块的AJAX提交数 ...
- Windows上Python3.5安装Scrapy(lxml)
常用网址: Python 3.5: https://www.python.org/downloads/ Wheel文件:http://www.lfd.uci.edu/~gohlke/pythonlib ...
- aspx和razor的区别
两者几乎都不懂,现在要选择一种,只能百度,然后一条一条看,也不知道诸位大神哪个说的对. 两个引擎语法完全不一样,性能上Asp.Net略占优势,语法糖则是razor的强项. 开发MVC3首选razor ...
- win7 安装 redis +php扩展
1:首先下载redis:redis-2.0.2.zip (32 bit),解压. 从下面地址下:http://code.google.com/p/servicestack/wiki/RedisWind ...