您的位置:首页 > 其它

leetcode 344

2016-05-02 18:07 399 查看
ResverString

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

C++

1、STL

class Solution {

public:

    string reverseString(string s) {

        reverse(s.begin(),s.end());

        return s;

        

    }

};

2、swap in-place

class Solution {

public:

    string reverseString(string s) {

        auto i=0;

        auto j=s.size();

        if(j==0)

            return s;

        else

            j--;

        while(i<j){

            swap(s[i++],s[j--]);

        }

        return s;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: