PU Reverse String

Jan 01, 1970

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

Example:

  • Given s = "hello", return "olleh".

Python Solution:

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s[::-1]

C Solution:

char* reverseString(char* s) {
    if (!s || !*s) return s;
    char *l = s, *r = l + strlen(s) - 1;
    while (l < r) {
        char c = *l;
        *l = *r;
        *r = c;
        l++;
        r--;
    }
    return s;
}

Summary:

  • nothing to say.

LeetCode: 344. Reverse String