[Lintcode]Reverse Words in a String
来源:互联网 发布:淘宝女装店铺起名 编辑:程序博客网 时间:2024/05/18 01:40
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
Clarification
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
- Reduce them to a single space in the reversed string.
class Solution {public: /** * @param s : A string * @return : A string */ string reverseWords(string s) { // write your code here string res=""; int len=s.size(); int start=0; while(start<len) { while(s[start]==' ') start++; if(start>=len) break; //注意后置0 int index=-1; index=s.find(" ",start); string tmp; if(index==-1) index=len; tmp=s.substr(start,index-start); if(res.size()==0) res=tmp; else res=tmp+" "+res; start=index+1; } return res; }};
0 0
- [Lintcode]Reverse Words in a String
- [Lintcode]Reverse Words in a String
- 【LintCode】Reverse Words in a String 翻转字符串
- 【LintCode】Reverse Words in a String 翻转字符串
- Reverse words in a string.
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- ANDROID ASSET STUDIO
- ~/vimrc配置
- AudioQueueStart Fail问题
- python读取xlsx文件
- 数据的标准化与中心化以及R语言中的scale详解
- [Lintcode]Reverse Words in a String
- JSONServlet 过滤器
- ServerSocketChannel与SocketChannel的使用
- Lua标准库 - 输入输出处理(input and output facilities)
- HDU 1372 HDU 1372
- API(APP会议直播)
- mysql innodb 数据安全性分析
- 记开博,立此为证——不求闻达于诸侯,但求精通程序之精髓
- iOS企业版发布 HTTPS证书以及服务器设置