LeetCode(151) Reverse Words in a String
2015-08-10 09:05
483 查看
[code]class Solution { public: void reverseWord(string &s, int index1, int index2) { index2--; while(index2 > index1) { char tmp = s[index2]; s[index2] = s[index1]; s[index1] = tmp; index1++; index2--; } return; } void removeSpace(string &s) { //int length = s.size(); int index = 0; while(index < s.size() && s[index] == ' ') s.erase(index, 1); while(index < s.size()) { while(s[index] != ' ') index++; index++; while(index < s.size() && s[index] == ' ') s.erase(index, 1); } while(s.size() >= 1 && s[s.size() - 1] == ' ') s.erase(s.size() - 1); } void reverseWords(string &s) { reverse(s.begin(), s.end()); s.push_back(' '); int length = s.size(); int index1 = 0; int index2 = 0; while(true) { while(index1 < length && s[index1] == ' ') index1++; if(index1 == length) break; index2 = index1; while(s[index2] != ' ') index2++; reverseWord(s, index1, index2); index1 = index2; } removeSpace(s); } };
相关文章推荐
- Word模板生成C#源码
- 利用Bundle来封装数据,然后用intent传递
- [CareerCup] 5.1 Insert Bits 插入位
- 3.2Library string Type
- hdoj1224Free DIY Tour【dp】
- An Interpretation of Logistic Regression
- UITableView
- jquery ui dialog autofocus 去掉默认第一个元素获取焦点
- opencv下摄像头的采集和保存
- 【Spring】Eclipse导入Spring包生成SpringMVC项目的注意事项
- POJ3468 A Simple Problem with Integers(线段树)
- Python于*args 和**kwargs使用
- Introduction to H2 Database
- Android-database
- Stockbroker Grapevine【POJ--1125】【Floyd】
- POJ 3267 The Cow Lexicon(DP)
- SqlServer表EXCEL数据复制的另一种方法
- Flask的部署
- OWIN概述
- UI08_TableView界面传值