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 ...
随机推荐
- commview for wifi 破解无线
相信了解无线网络的读者都知道安全性是无线网络的先天不足,正是因为他的传播通过空气,所以信号很容易出现外泄问题,相比有线网络来说信号监听变得非常简单. 部分用户通过WEP加密的方式来保护网络通讯数据包避 ...
- swift3.0 hello swift(1)
一直对swift感兴趣,在前段时间的新闻中,大多是swift3.0发布和xcode8.0的改进,因为改动比较大,以前使用swift2.x做项目的人,都在担心其项目从2.x迁移到3.0+的问题.以前简单 ...
- Dropping tests(01分数规划)
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8176 Accepted: 2862 De ...
- iOS开展-CocoaPods安装和使用教程
原文链接: iOS开展-CocoaPods安装和使用教程 修正已经增加了自己的理解. CocoaPods安装和使用教程 Code4App 原创文章.转载请注明出处:http://code4app.co ...
- Cocos2d-x 3.0 使用TinyXml 解析XML文件
在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...
- Java输入输出流(1)
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java全部的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...
- UVa----------1594(Ducci Sequence)
题目: 1594 - Ducci Sequence Asia - Seoul - 2009/2010A Ducci sequence is a sequence of n-tuples of inte ...
- Android 开发笔记 “SharePreference 数据存取”
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- C语言之辗转相除法
最大公约数和最小公倍数 从键盘输入两个正整数,求出其最大公约数和最小公倍数.代码如下: #include<stdio.h>int ss(int);int main(void){ int a ...
- Hadoop_Lucene
http://codelife.me/blog/2012/11/03/jackson-polymorphic-deserialization/ http://itindex.net/blog/2012 ...