344. Reverse String

来源:互联网 发布:大掌柜物流软件 编辑:程序博客网 时间:2024/06/06 18:34

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

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

Subscribe to see which companies asked this question

临时对象

class Solution {public:    string reverseString(string s) {        int length=s.size();        string str_tmp(length,0);        int j=0;        for(int i=s.size()-1;i>=0;i--)        {            str_tmp[j++]=s[i];        }                return str_tmp;    }};


原址交换

class Solution {public:    string reverseString(string s) {        int j=s.size()-1;        for(int i=0;i<j;i++,j--)        {            swap(s[i],s[j]);        }                return s;    }};

牛逼的C++


class Solution {public:    string reverseString(string s) {       return string(s.rbegin(), s.rend());    }};







0 0
原创粉丝点击