Reverse Words in a String
来源:互联网 发布:淘宝卖点卡 编辑:程序博客网 时间:2024/05/17 01:51
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
- 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
原题地址Reverse words in a string
class Solution {public:void reverseWords(string &s) {stack<string> strstack;int start = 0;int length = s.length();string str;while ( (start < length) && (s.at(start) == ' ')) //去掉头部的空格{start++;}for (int i = start; i < length; ){if (s.at(i) != ' '){i++;}else{str = s.substr(start,i-start);strstack.push(str);while ( (i < length) && (s.at(i) == ' '))//去掉单词之间的多个空格{i++;}start = i;}}if (length != start)//防止尾部出现空格{strstack.push(s.substr(start,length-start));}s = "";while (!strstack.empty()){s = s+ strstack.top();strstack.pop();if (!strstack.empty()){s = s + " ";}}}};
0 0
- 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
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Commons Configuration使用
- 出现<authentication mode="Windows"/>错误解决办法
- 的发表的发表的发表
- HDU 3966 Aragorn's Story 树链剖分
- 速度vbsdvsd
- Reverse Words in a String
- Cocos2d-x3.1通过JniHelper类调用java中非静态函数
- Git与Repo入门
- TreeSet有趣问题之add方法原理
- 第四章 通过示例探索 F# 和 .NET 库
- Android开发资源推荐第2季
- Hadoop-2.4.0安装和wordcount运行验证
- linux下服务器命令小结【持续更新】
- 使用CURL封装HttpClient