Leetcode - Two sums

Category: python (6 Views)

For this problem, two integers should add up to the target. Also we have to return the indices and not the integers.

This is my solution for Leetcode's two sums problem. My output and expected output look exactly the same (except the whitespace after comma but even if I considered that, the problem remained still). Do you see what's wrong?

nums = [2,7,11,15] and target = 9

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        
        output = []

        for i in range(len(nums)):
            for j in range(i+1, len(nums)): #i+1 to not iterate over same elements 
                if nums[i] + nums[j] == target:
                    output.append(nums.index(nums[i]))
                    output.append(nums.index(nums[j]))
                    print(output)
🔴 No definitive solution yet