344. Reverse String
2016-05-13 11:12
337 查看
344. Reverse String
Description:Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
Link:
https://leetcode.com/problems/reverse-string/
Analysis:
这种问题第一反应就是使用递归来解决,这里我使用stack来实现的。
Source Code(C++):
#include <iostream> #include <string> #include <stack> using namespace std; class Solution { public: string reverseString(string s) { stack<char> a; for(int i=0; i<s.length(); i++) { a.push(s.at(i)); } string s_reverse; while(!a.empty()) { s_reverse += a.top(); a.pop(); } return s_reverse; } }; int main() { Solution a; string str = "hello"; cout << a.reverseString(str); return 0; }
相关文章推荐
- gulp详细入门教程
- 谈谈iOS中粘性动画以及果冻效果的实现
- 游戏植入广告获取收益
- javascript 常用实用函数。。。。。。
- java实现二分查找
- session管理
- java的五种数据类型解析
- 信号量与互斥锁
- linux环境知识点备忘录
- java中的泛型
- windows下的汇编学习
- debian8下配置postgresql9.5.2、pgpool3.5.2、heartbeat3.0.5的HA热备
- ioctl函数
- UIKit
- linux 下mysql卸载
- UIWebView与JS的深度交互
- JS打开窗口的2种方式d的区别
- 使用ifstream对象逐行读取.txt文件,并使用stringsream拆分各个变量
- 面试题之字符串反转
- 数据库分库和分表