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 ...
随机推荐
- tablbView中section的间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (sect ...
- python给多个发送邮件附件,参考于《python自动化运维》
#!/usr/bin/env python #coding: utf-8 #author:luodi date:2015/02/12 #description:this is a send mail ...
- window.location.href("url") 无法在chrome和Firefoxz中使用
今天在js代码中加了一句window.location.href(‘url’)希望实现页面的跳转,IE中可以正常使用,但是Firefox却提示window.location is not a func ...
- Hibernate注解学习1
由于项目的原因目前需要一些简单注解,于是就做了个hibernate注解的小demo,来记录一下. 1.需要引入jar包ejb3-persistence.jarhibernate-annotations ...
- js 数组排除重复值(string)
前提:数组中的元素类型为:string 在网上看了许多高大尚的文章,还是解决不了我的string arry 的问题,只能怪自己脑残了,上代码: <!DOCTYPE html> <ht ...
- 如何用EXCEL表计算今天是本年的第几周?
单元格内输入如下代码计算出来的数字即是当日在本年度的第几周,如下: =INT((TODAY()-DATE(YEAR(TODAY()),1,1)-WEEKDAY(DATE(YEAR(TODAY()),1 ...
- css3弹性盒模型(Flexbox)
Flexbox是布局模块,而不是一个简单的属性,它包含父元素和子元素的属性. Flexbox布局的主体思想是似的元素可以改变大小以适应可用空间,当可用空间变大,Flex元素将伸展大小以填充可用空间,当 ...
- javascript 小计
①if文 if(){} else if(){} else if 中间有空格 ②
- Objextive-C几道小题目笔记
//掷骰子题,掷骰子100次,输出每个号出现的次数 void one() { for (int i=1; i<=100; i++) { int a = arc4random() % 6 +1; ...
- C语言--基本运算符
一.算术运算符 1. 加法运算符 + * 除了可以进行加法运算外,还可以表示正号:+521 2.减法运算符 — * 除了可以进行减法运算外,还可以表示负号:—741 3.乘法运算法符 * * 请注意符 ...