Home > database >  Leetcode problem asks for the output to be in List[List[int]] but returns a set without converting i
Leetcode problem asks for the output to be in List[List[int]] but returns a set without converting i

Time:01-12

This is a solution to a leetcode problem. My problem is with why the output below is acceptable? The problem is expecting List[List[int]] but the "res" that is being returned is a set.

I don't know if python3 has anything to do with this but I couldn't find anything online. Your help is much appreciated.

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        res, dups = set(), set()
        seen = {}
        for i, val1 in enumerate(nums):
            if val1 not in dups:
                dups.add(val1)
                for j, val2 in enumerate(nums[i 1:]):
                    complement = -val1 - val2
                    if complement in seen and seen[complement] == i:
                        res.add(tuple(sorted((val1, val2, complement))))
                    seen[val2] = i
        return res

CodePudding user response:

Typings in python aren't enforced in any way. You can write

def f() -> int:
    return "abc"

f()

And everything will work smoothly (you will be warned by some linters like mypy, but nothing that stops this from executing)

So there is no problem on your side with the code. But the second question is, what leetcode does to check your answer? Probably they doing something like:

for single_list in threeSum(...): # they call your function
    <check if single_list is correct>

Notice that it doesn't matter do you returned set, list, tuple or any other iterable - such checker would work for any of them until it contains what's expected.

  • Related