344. Reverse String

Problem

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

Example

Input: "hello"
Output: "olleh"
Input: "A man, a plan, a canal: Panama"
Output: "amanaP :lanac a ,nalp a ,nam A"

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    string reverseString(string s) {
        string res = "";
        for(int i=s.size()-1;i>=0;i--)
            res += s[i];
        return res;
    }
};

Result

344. Reverse String.png

你可能感兴趣的:(344. Reverse String)