151. Reverse Words in a String
Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

分析:我用的方法是把字符串中的所有单词放入栈里,然后将栈里的所有字符串弹栈到字符串s中~

class Solution {
public:void reverseWords(string &s) {stack<string> sstack;int flag = 0;string temp = "";for(int i = 0; i < s.length(); i++) {if(s[i] != ' ' && flag == 0) {temp = "";temp += s[i];flag = 1;} else if(s[i] != ' ') {temp += s[i];} else if(s[i] == ' ' && flag == 1){sstack.push(temp);flag = 0;}if(i == s.length() - 1 && flag == 1)sstack.push(temp);}s = "";while(!sstack.empty()) {string temp = sstack.top();s += temp;sstack.pop();if(!sstack.empty())s += " ";}}
};

LeetCode 151. Reverse Words in a String相关推荐

  1. leetcode 151 Reverse Words in a String (python)

    leetcode 151   Reverse Words in a String 题目描述: Given an input string, reverse the string word by wor ...

  2. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public:string reverseWords(string ...

  3. leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li

    以前从来没做过什么oj,发现做oj和在本地写代码或者纸上写差别还是很大的,觉得今天开始刷oj,特此记录一下. 1.Reverse Words in a String Given an input st ...

  4. LeetCode 345. Reverse Vowels of a String

    题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  5. LeetCode 557. Reverse Words in a String III

    题目 : Given a string, you need to reverse the order of characters in each word within a sentence whil ...

  6. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  7. LeetCode OJ1:Reverse Words in a String

    问题描述: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...

  8. Leetcode 345: Reverse Vowels of a String

    问题描述: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ...

  9. 151. Reverse Words in a String

    1 题目理解 输入:一个字符串s 规则:一个单词是一串非空字符组成的.单词之间用空格分隔. 输出:将字符串按照单词反转字符串.多余的空格只保留一个. Example 1: Input: s = &qu ...

最新文章

  1. mysql指定当前时间为默认时间_MySQL设置当前时间为默认值的方法
  2. python实现案例_基于Python实现相关分析案例
  3. 【Fragment】onActivityResult
  4. 4.2w Star的文件同步工具,比VIP网盘还好用!
  5. python现在好找工作吗-转行学Python好找工作吗?
  6. Java论坛系统 JForum
  7. jaxb 命名空间_在JAXB解组期间应用名称空间
  8. 学会这6个强大的CSS选择器,将真正帮你写出干净的CSS代码!
  9. php取汉字拼音首字母,php获取汉字拼音首字母的函数(真正可以使用的)
  10. 改用MyAnalyzer的KMeans聚类算法
  11. Vivado入门使用指南之----按键消抖(仿真与在线逻辑分析仪仿真-不使用ip)
  12. 服务器中C盘的虚拟大文件,查看虚拟机的大文件系统
  13. 无限分级函数 简单 引用绑值
  14. 【第四课】UAV倾斜摄影测量三维建模技术软件——Smart 3d
  15. 演讲稿:遇见下班加油站,开启改变之路(不喜者勿入)
  16. python小白使用pycharm新建项目,import什么内置包都报错
  17. nmcli命令详解>>>创建热点,连接wifi,管理连接等
  18. Jmeter Kafka插件开发之Sampler篇
  19. CS5266中文规格书|Capstone CS5266中文设计资料|TYPEC转HDMI带PD3.0+USB3.1拓展坞转换方案资料
  20. 苹果官方iCloud中的照片如何迁移到Google Photo教程来了

热门文章

  1. SQL PASS西雅图之行——University of Washington篇
  2. 去除字符串中的html标记
  3. 顺应大数据时代创新社会治理模式
  4. 《从零开始学Swift》学习笔记(Day 14)——字符串的插入、删除和替换
  5. Zabbix 监控LVS连接的状态
  6. CCProgressTimer进度条效果
  7. 方法参数关键字:params、ref及out
  8. .NET下,你采用的哪种方式来操作数据库
  9. Intel、AMD和Arm 告警:注意新的推断执行CPU漏洞!
  10. 苹果修复老旧设备中的两个 iOS 0day