题目:将一个英文句子翻转,比如: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. 在EasyUI中统一判断是否有选中行,如果有则将选中行数据传入回调函数

    function procossWithSeletedData(func) { var rowData = $("#tbGrid").datagrid("getSelec ...

  2. resolv.conf 是什么

    From Wikipedia, the free encyclopedia This article does not cite any references or sources. Please h ...

  3. SQL查询语句47题

    select * from student select * from score --select * from grade select * from course select * from t ...

  4. JQ动画,背景透明度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Reverse Words in a String (JAVA)

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. volatile 和const 变量的使用

    一.volatile定义: 一个定义为volatile的变量是说这变量可能会被意想不到的被改变,这样,有了volatile变量后,就提醒编译器就不会去假设这个变量的值了.精确地说就是,编译中的优化器在 ...

  8. Android Studio .grade文件知识

    1.位置 2.问题:当AndroidStudio版本不同的时候,导致.gradle文件版本不同(当前为2.10),因为AndroidStudio升级的时候会自动升级.gradle.这样通过svn加载到 ...

  9. C++有没有string转化int的函数,怎样转换

    有两种方法1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib>#include <string> std::st ...

  10. nginx请求体读取

    上节说到nginx核心本身不会主动读取请求体,这个工作是交给请求处理阶段的模块来做,但是nginx核心提供了ngx_http_read_client_request_body()接口来读取请求体,另外 ...