Python solution can not pass test case

  • 0

    Hi guys, my python code is as follows:

    import numpy as np
    class Solution(object):
        def twoSum(self, numbers, target):
            :type numbers: List[int]
            :type target: int
            :rtype: List[int]
            lower = list(filter(lambda x: x < target/2,numbers))
            upper = (target - np.array(numbers[len(lower):])).tolist()
            result = list(set(upper).intersection(lower))
            index1 = 1 + numbers.index(result[0])
            index2 = 1 + numbers.index(target - result[0])
            return [index1, index2]

    I submitted that and it shows:

    Runtime Error Message:
    Line 12: IndexError: list index out of range
    Last executed input: [-1,0],  -1

    I tested that on my PC, it is fine, can someone tell me where is the problem?

  • 0

    target = -1, target/2 = -1 Is this the number you want?

Log in to reply

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