LeetCode::Reverse Words in a String
来源:互联网 发布:手机振动器软件 编辑:程序博客网 时间:2024/06/05 06:30
#include<iostream>#include<string>using namespace std;class Solution {public: void reverseWords(string &s) { int i, n;string tmp_s;int word_Length = 0;if(s.empty())return ;for(i = s.length()-1; i >= 0;){word_Length = 0;while(s[i] == ' ')i--;n = i;while(s[n] != ' ' && n >= 0){word_Length++;n--;}n += 1;tmp_s += s.substr(n,word_Length);tmp_s += ' ';i -= word_Length;}cout << "tmp_s.length() = " << tmp_s.length() << endl;/*去掉尾空格*/i = tmp_s.length()-1;while(tmp_s[i] == ' ' && i >= 0)i--;cout << "i = " << i << endl;tmp_s.resize(i+1);cout << "tmp_s.length() = " << tmp_s.length() << endl;cout << "tmp_s = " << tmp_s << endl; <span style="white-space:pre"></span>s = tmp_s; //cout << tmp_s.append("space") << endl;//s = tmp_s.substr(0,tmp_s.length()); }};int main(){string s;Solution reverseSolution;getline(cin,s);cout << s << endl;cout << "s.length() = " << s.length() << endl;reverseSolution.reverseWords(s);cout << s << endl;cout << "s.length() = " << s.length() << endl;return 0;}
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
- 30分钟了解C++11新特性
- Android 4.4.2 bluetooth解析(一)
- 什么是sys文件系统
- express框架中,session的使用方法
- 网络资源
- LeetCode::Reverse Words in a String
- 第十四周项目1-动物这样叫
- Win7系统重启总是进启用启动修复
- 第13周——动物叫
- GCD 深入理解
- 人活着的一百个必然会遭遇到的疑问
- 今天开了个博客。
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- 第十四周项目一 动物这样叫(1)