LeetCode 151. Reverse Words in a String
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相关推荐
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode OJ1:Reverse Words in a String
问题描述: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...
- 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', ...
- 151. Reverse Words in a String
1 题目理解 输入:一个字符串s 规则:一个单词是一串非空字符组成的.单词之间用空格分隔. 输出:将字符串按照单词反转字符串.多余的空格只保留一个. Example 1: Input: s = &qu ...
最新文章
- mysql指定当前时间为默认时间_MySQL设置当前时间为默认值的方法
- python实现案例_基于Python实现相关分析案例
- 【Fragment】onActivityResult
- 4.2w Star的文件同步工具,比VIP网盘还好用!
- python现在好找工作吗-转行学Python好找工作吗?
- Java论坛系统 JForum
- jaxb 命名空间_在JAXB解组期间应用名称空间
- 学会这6个强大的CSS选择器,将真正帮你写出干净的CSS代码!
- php取汉字拼音首字母,php获取汉字拼音首字母的函数(真正可以使用的)
- 改用MyAnalyzer的KMeans聚类算法
- Vivado入门使用指南之----按键消抖(仿真与在线逻辑分析仪仿真-不使用ip)
- 服务器中C盘的虚拟大文件,查看虚拟机的大文件系统
- 无限分级函数 简单 引用绑值
- 【第四课】UAV倾斜摄影测量三维建模技术软件——Smart 3d
- 演讲稿:遇见下班加油站,开启改变之路(不喜者勿入)
- python小白使用pycharm新建项目,import什么内置包都报错
- nmcli命令详解>>>创建热点,连接wifi,管理连接等
- Jmeter Kafka插件开发之Sampler篇
- CS5266中文规格书|Capstone CS5266中文设计资料|TYPEC转HDMI带PD3.0+USB3.1拓展坞转换方案资料
- 苹果官方iCloud中的照片如何迁移到Google Photo教程来了