Reverse String——String

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

Example:
Given s = "hello", return "olleh".

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        
        L = list(s)
        L.reverse()
        return ''.join(L)
        """
        start, end = 0, len(s)-1
        if len(s) <= 0:
            return ''
        sl = list(s)
        while start < end:
        	t = sl[start]
        	sl[start] = sl[end]
        	sl[end] = t 
        	start += 1
        	end -= 1
        return ''.join(sl) 

 

猜你喜欢

转载自qu66q.iteye.com/blog/2316912