leetcode Reverse Words in a String
2015-12-09 20:04
387 查看
原题链接:https://leetcode.com/problems/reverse-words-in-a-string/
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
Description
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) { n = s.length(); string x = ""; reverse(s.begin(), s.end()); while (!op.empty()) op.pop(); for (size_t i = 0; i < n;) { while (i < n && s[i] == ' ') i++; if (i == n) break; while (i < n && s[i] != ' ') { op.push(s[i++]); } while (!op.empty()) { x += op.top(); op.pop(); } x += ' '; } if (x.length()) x.pop_back(); s = x; } private: size_t n; stack<char> op; };
相关文章推荐
- 对于js浮点数误差问题 完美解决办法
- 乱序字符串_LintCode
- ZIGBEE中SerialApp_ProcessEvent分析
- Android静态变量的生命周期
- 这两天都忘了写了,养成一个习惯真不容易
- Android控件之ListView ChoickModel 单选、多选模式
- Leetcode190: Game of Life
- 深入理解计算机系统中网络编程一节echo客户端服务器的源码编译和运行
- linux用户态和内核态理解
- 弹性ListView
- 大转盘游戏
- XMPP系列 - 01 - 即时通信简述
- 看看什么是爱,见心痛
- MySQL之----在java编程加强知识点
- leetcode Min Stack
- 1191: [HNOI2006]超级英雄Hero
- wordCount的认识(java版)
- SAP接口编程-RFC系列04 : 动态调用SAP函数
- 关于LinkedList的操作--重点
- 几个java代码优化注意点