344 Reverse String Reverse String

Please write a function whose function is to reverse the input string.
Example:
Input: s = "hello"
Return: "olleh"
See: https://leetcode.com/problems/reverse-string/description/
C++:

class Solution {
public:
    string reverseString(string s) {
        int n=s.size();
        if(n==0||s.empty())
        {
            return "";
        }
        int left=0,right=n-1;
        while(left<right)
        {
            char tmp=s[left];
            s[left]=s[right];
            s[right]=tmp;
            ++left;
            --right;
        }
        return s;
    }
};

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324400385&siteId=291194637