Leetcode 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
".
此题要注意的几个情况是:
(1)输入字符串可能含有前缀0和后缀0
(2)字符串中每个单词之间可能含有多个空格
(3)字符串是空串
(3)字符串只有一个单词
此题主要是根据空格分隔单词,然后将分隔后单词的顺序逆转一下即可
void reverseWords(string &s){
string buf;
stringstream ss(s);
vector<string> word;
while(ss >> buf) word.push_back(buf);
if(word.size() == ) s="";
else{
s="";
int n = word.size();
for(int i = n -; i >=; -- i){
if(i!=n-) s+=" ";
s+=word[i];
}
}
}
Leetcode Reverse Words in a String的更多相关文章
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...
- [leetcode]Reverse Words in a String @ Python
原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...
随机推荐
- Android ANR分析(1)
转自:http://blog.csdn.net/itachi85/article/details/6918761 一:什么是ANR ANR:Application Not Responding,即应用 ...
- 谈谈Delphi中的类和对象3---抽象类和它的实例
四.抽象类和它的实例 Delphi中有一个类称为是抽象类,你不能天真的直接为它创建一个实例,如 var StrLst: TString; begin StrLst:= TString.Create; ...
- ytu 1058: 三角形面积(带参的宏 练习)
1058: 三角形面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 190 Solved: 128[Submit][Status][Web Boar ...
- C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
tfref 前言 C++对象的内存布局 只有数据成员的对象 没有虚函数的对象 拥有仅一个虚函数的对象 拥有多个虚函数的对象 单继承且本身不存在虚函数的继承类的内存布局 本身不存在虚函数(不严谨)但存在 ...
- Quartz:Cron Expressions
原文地址:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontrigger.html 注意: 位也可能是7位, ...
- PMP 第九章 项目人力资源管理
1制定人力资源计划 2组建项目团队 3建设项目团队 4管理项目团队 1.规划人力资源管理的作用是什么?组织图和职位描述的表现形式有哪些?RAM和RACI的关系是什么?人力资源管理计划的内容有哪些? 人 ...
- WPF之MVVM(Step1)——自己实现ICommand接口
开发WPF应用程序,就不得不提MVVM.下面偶将展示MVVM中简单的实现,其中主要在于ICommand的实现上,不过这种实现方式,应该不会有多少人在开发中使用,在此仅作学习使用. 准备: 界面绘制,简 ...
- 【设计模式】MVC模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO.它 ...
- 在MySQL中存储大文件
我们的目标:把一首mp3保存到MySQL数据库中! 由于MySQL默认当存入的数据太大时会抛异常,所以应在my.ini中添加如下配置!max_allowed_packet=10485760,这样,可以 ...
- minix3(一)安装以及编辑文件
作为一条通信狗,最近开始自学操作系统.听说用MINIX3学操作系统很好,就决定跟UCSB的课程试试. 首先在虚拟机上安装MINIX3. 开始用的VM Station,按照百度文库里安装minix3的教 ...