My recursive Python


  • 0
    C

    I wonder if it is okay for this problem.

    enter code hereclass Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return -1
        else:
            return self.helper(nums, 0, len(nums) - 1)
    
    def helper(self, nums, i, j):
        if i == j:
            return nums[i]
        elif nums[i] < nums[j]:
            return nums[i]
        elif j - i == 1:
            if nums[i] <= nums[j]:
                return nums[i]
            else:
                return nums[j]
        else:
            m = (i + j) / 2
            if nums[m] < nums[j]:
                return self.helper(nums, i, m)
            elif nums[m] > nums[j]:
                return self.helper(nums, m, j)
            else: # == cannot know left or right
                return self.helper(nums, i, j-1)

Log in to reply
 

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