344. Reverse String

来源:互联网 发布:js正则表达式标点符号 编辑:程序博客网 时间:2024/05/17 07:14

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

Example:

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


#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
    string reverseString(string s) {
 /*       string ss;
        
      
        for(int i=0,j=s.length()-1;i<s.length();i++,j--)
        {
        ss[i] = s[j];
}
        
        return ss;
        
        for(int i=0;i<ss.length();i++)
        cout << ss[i];
    */
    
     int i = 0, j = s.size() - 1;
        while(i < j){
            swap(s[i++], s[j--]); 
        }
        
        return s;
    
    }
};


int main()
{
Solution s;
string r;
r = "hello";
cout << s.reverseString(r) << endl;

return 0;
}

0 0
原创粉丝点击