[leetcode] Reverse Words in a String
来源:互联网 发布:java 加载资源文件 编辑:程序博客网 时间:2024/05/22 14:02
From : https://leetcode.com/problems/reverse-words-in-a-string/
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
class Solution {public: void reverseWords(string &s) { stack<string> stc;string tstr="";s += ' ';int len = s.length();for(int i=0; i<len; i++) {if(i==0 && s[i]==' ') continue;if(s[i]==' ' && s[i-1]==' ') continue;if(s[i]==' ' && tstr!="") {stc.push(tstr);tstr = "";} else {tstr += s[i];}}s="";while(!stc.empty()) {s += stc.top();stc.pop();if(stc.empty()) {break;}s += " ";} }};Solution 2:
class Solution {public: void reverseWords(string &s) { string ts;int end = s.size()-1, start = end;while(end>=0) {while(end>=0 && s[end]==' ') end--;start = end;while(start>=0 && s[start]!=' ') start--;if(start!=0 && start<end && ts!="") ts.append(" ");if(start < end) ts.append(s.substr(start+1, end-start));end = start;}s = ts; }};
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
- 3-44(期末考试成绩在某个阶段的人数)
- Cpp DS--单链表
- StringUtils.isEmpty和StringUtils.isBlank用法
- 录制视频
- JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解
- [leetcode] Reverse Words in a String
- CentOS 7 安装l2tp vpn,配置客户端,并使用net-speeder优化速度
- quartusII综合报错(Error (10133): Verilog HDL Expression error at lcd_display_wsfeng.v(37): illegal part)
- 字符串反转
- icvConvertToFastHaarFeature
- 使用wecenter&SAE搭建问答社区型网站
- 【黑马程序员】---java概述
- java中的多线程一:实现多线程
- HDU3172---Virtual Friends