My python solutions


  • 0
    F

    class Solution(object):
    """Reverse String class."""

    def reverse_string_by_position(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s[::-1]
    
    def reverse_string_recursive(self, s):
    	'''
        Reverse a string recursively
    
        :param s: A string
        :type s: str
        :rtype: str
        '''
        if len(s) < 1:
            return s
        return s[-1] + self.reverse_string_recursive(s[:-1])
    
    def reverse_string_iteratively(self, string):
    	"""
        Reverse a string recursively
    
        :param s: A string
        :type s: str
        :rtype: str
        """
        length = len(string)
        result = list(string)
        for i in range(length / 2):
            result[i] = string[length - 1]
            result[length - 1] = string[i]
            length = length - 1
        return ''.join(result)
    
    def reverse_string_using_built_in_method(self, s):
    	"""
        Reverse a string using built in method
    
        :param s: A string
        :type s: str
        :rype: str
        """
        return ''.join(reversed(s))

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.