leetcode 344

来源:互联网 发布:科脉软件 编辑:程序博客网 时间:2024/05/21 08:44

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;
    }
};

0 0