Reverse Words in a String | LeetCode OJ | C++
我的思路:先读取每一个单词,存放到容器中;读取完毕后,将容器中的单词倒序写入输出中。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void f(string &s){
vector<string> vs;
string temp="";
int i=;
while(s[i]!='\0'){ //遍历直到结尾
if (s[i]==' '){ //遇到空格
if (!temp.empty())
vs.push_back(temp); //单词放入容器中
++i; // 跳过空格
temp=""; }
else{
temp.push_back(s[i]); //将字母加到temp字符串后面
++i;
}
}
if (!temp.empty())
vs.push_back(temp); //将最后一个单词放入容器中 int len=vs.size(); //倒序遍历容器
string res="";
for(int j=;j<len;++j){
res.append(vs[len-j-]);
if(j!=len-)
res.push_back(' ');
}
s=res;
} int main(){
string s;
getline(cin,s);
f(s);
cout<<s<<endl;
}
以上只是一种实现方法,还应该有更加高效的方法,欢迎讨论。
Reverse Words in a String | LeetCode OJ | C++的更多相关文章
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- Reverse Words in a String——LeetCode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Reverse Words in a String leetcode java
题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 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 ...
随机推荐
- JS动态增加页面上的控件实例
<input type="button" value="继续添加" onclick="append();"/ > & ...
- Cstyle的札记,Freertos内核具体解释,第0篇
Freertos是一个硬实时内核,支持众多的微处理器架构,我们能够从它的官网(www.freertos.ort)下载它的sourcecode,同一时候也能够看出它支持了几十种的微处理器架构,这 ...
- poj2492 A Bug's Life【基础种类并查集】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298148.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- js字符串大小写转换
var str = 'hello world'; alert(str.toUpperCase());//将字符串转换为大写. alert(str.toLowerCase());//将字符串转换为小写. ...
- javascript对象属性——数据属性和访问器属性
ECMA-262第五版在定义时,描述了属性property的各种特征,定义这些特性是为了实现javascript引擎用的,为了表示该特性是内部值,规范把它们放在了两对儿方括号中,例如[[Enumera ...
- Mac Yosemite下Android Studio环境问题集合
1. java not found 在mac Yosemite下,因jre升级到1.8,导致Android Studio无法启动.报错:"JAVA not found". 解决方法 ...
- WCF跟踪分析 使用(SvcTraceViewer)
1.首先在WCF服务端配置文件中配置两处,用于记录WCF调用记录! A:<system.serviceModel>目录下: <diagnostics> <mes ...
- Sed简介 (转)
Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓 ...
- shell实例浅谈之六文件特定行打印的多种方法
一.问题 Sed和AWK在处理文件方面有很强的优势,还有head和tail等文件处理工具的使用,grep也可实现文本的搜索.上述命令都可以在后面直接加文件名,不需要在前面使用cat添加管道,cat会影 ...
- 关于Ubuntu12.04下code::blocks不能使用debug解决方法
问题描述: 系统:ubuntu 12.04 code::blocks版本:10.05 问题现象:debug->start 之后出现:warning: GDB: Fail ...