LeetCode:Reverse Words in a String
来源:互联网 发布:网络语言吃鸡什么意思 编辑:程序博客网 时间:2024/05/17 08:16
题目:
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.
解题思路:
从头到尾将每个单词提取出来,反向赋值就行了。由于栈具有“先进后出”的性质,所以我用的栈实现。
class Solution {public: void reverseWords(string &s) { stack<string> stk; istringstream in(s); string tmp; while( in >> tmp) stk.push(tmp); s = ""; while(!stk.empty()) { s +=stk.top(); if(stk.size()!=1) s +=" "; stk.pop(); } }};
0 0
- 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
- LeetCode(151)Reverse Words in a String
- Reverse Words in a String -- LeetCode
- leetcode Reverse Words in a String
- LeetCode OJ:Reverse Words in a String
- LeetCode Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- [Leetcode] Reverse Words in a String
- Reverse Words in a String[LeetCode]
- LeetCode OJ :Reverse Words in a String
- Leetcode: Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- Assert断言
- 成绩最大
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- 显示Mac隐藏文件
- LeetCode:Reverse Words in a String
- 给网站添加https访问连接
- Async详解之一:流程控制
- dwr中的session error问题解决
- Android开发-从硬件到APP(一) : 单独编内核驱动模块
- 感动前行——给医学媳妇写的演讲稿(非IT类)
- 微内核介绍
- Dijkstra和Floyd算法
- ARM汇编学习(1)