Leetcode# Reverse String

来源:互联网 发布:网络负面新闻消除方案 编辑:程序博客网 时间:2024/06/01 16:59

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

简单题,本来想用c++里面的algorithm库里的reverse()函数反转字符串呢,结果提示函数有问题,刚开始在leetcode刷题,不知道怎么导入函数。索性自己写吧

我的代码:

class Solution {public:    string reverseString(string s)     {       string ss="";       for(int i=s.size()-1;i>=0;i--)           ss+=s[i];        return ss;    }};
附上翻转函数:

#include <iostream>#include <string>#include <algorithm>using namespace std;int main(){    string s = "hello";    reverse(s.begin(),s.end());    cout<<s<<endl;    return 0;}