Leetcode OJ : Reverse Words in a String
来源:互联网 发布:淘宝店铺装修助手 编辑:程序博客网 时间:2024/05/06 13:52
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
click to show clarification.
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.
这一题和PAT里的那个题目基本是一样的,http://blog.csdn.net/xiaqunfeng123/article/details/19989735
代码如下:
class Solution {public: void reverseWords(string &s) { stack<string> str; string word,temp; bool flag=false; stringstream ss(s); while(ss>>word) str.push(word); while(!str.empty()) { if(flag) temp+=" "; else flag=true; temp+=str.top(); str.pop(); } s=temp; }};
0 0
- LeetCode OJ:Reverse Words in a String
- LeetCode OJ :Reverse Words in a String
- Reverse Words in a String -LeetCode OJ
- Leetcode OJ : Reverse Words in a String
- [LeetCode OJ] Reverse Words in a String
- LeetCode OJ:Reverse Words in a String
- LeetCode OJ : Reverse Words in a String
- LeetCode OJ Reverse Words in a String
- LeetCode OJ --问题与解答 Reverse Words in a String
- [LeetCode OJ] Reverse Words in a String 解题报告
- <LeetCode OJ> 151. Reverse Words in a String
- [OJ]: Reverse Words in a String
- Leetcode - string - Reverse Words in a String
- Reverse words in a String leetcode
- [LeetCode]Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- Reverse Words in a String (LeetCode)
- LeetCode Reverse Words in a String Total
- shutdown关机
- 使用register_chrdev注册字符设备
- hdu4604 不错的子序列问题
- 使用cv::findFundamentalMat要注意的几点
- chrome firefox 访问 www.google.com 被劫持
- Leetcode OJ : Reverse Words in a String
- 第六周作业1——利用哈夫曼编码英文字母表
- Eclipse中10个最有用的快捷键组合
- HDU 数塔 (dp入门经典)
- 在eclipse中创建web项目(非myeclipse)
- jQuery ajax Load关闭缓存的方法
- 浏览器兼容性问题
- struts1自定义标签
- 使用cdev_add注册字符设备