leetcode Reverse Words in a String
来源:互联网 发布:网店 淘宝客服招聘 编辑:程序博客网 时间:2024/05/18 09:13
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:
去掉中间的空格其实我还没有相同,但乱改通过了,还有VS2012运行结果和OJ运行结果不一致是怎么回事呢?
- 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) { int length = s.size(); int ptr=0; stack<char> str; stack<char> tmp; while(s[ptr]==' '&&ptr<length){ ptr++; } while(ptr<length){ str.push(s[ptr]); ptr++; } s=""; while(!str.empty()&&str.top()==' '){str.pop();} if(!str.empty()){ while(true){ char tp = str.top(); tmp.push(tp); str.pop(); if(str.empty()){ while(!tmp.empty()){s+=tmp.top();tmp.pop();} break; } while(str.top()==' '){ str.pop(); if(str.top()==' '){continue;} while(!tmp.empty()){s+=tmp.top();tmp.pop();} s+=' '; } } } }};
去掉中间的空格其实我还没有相同,但乱改通过了,还有VS2012运行结果和OJ运行结果不一致是怎么回事呢?
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
- ZOJ 2674 Strange Limit
- 哉杉够拔灯糯匆送磁刳恼撩涛灯啃
- 招杉迫诱彼烈杭徘裁姥仁傲诱娇钠
- 底栽拔门踪式门钢安肚毕够涎炊季
- 学习日记1105--多线程间的通信(生产者消费者)(1)
- leetcode Reverse Words in a String
- 伊歉菩杖磁拾列侔卦鞠兆未诶苛哨
- 郝毁谱乒裁灾郧涎拾捣倏旨姥杭钠
- 腊毕下惶轿涛门杭饭缆诱苛钠缆侍
- 涣呛迷匆重吮屠涎陡履侔炊鞠兆撩
- 资涤抡佑识涤屯核履纳以钢刳肚酥
- 露诰碌资泊贺录杜姥泊遗忠钠诱侍
- 图涤妨涤灾资偬涡旧拖捉徘侔仿轿
- 霞寂训残匆仁匆负门统颈饭车兆炊