sub two_sum_choroba {
my ($input, $target) = @_;
my %seen;
for my $i (0 .. $#$input) {
my $x = $input->[$i];
return [$i, $seen{ $target - $x }]
if exists $seen{ $target - $x };
$seen{$x} = $i;
}
}
####
Runtime: 48 ms, faster than 99.40% of Python3 online submissions for Two Sum.
Memory Usage: 15.4 MB, less than 42.44% of Python3 online submissions for Two Sum.
####
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = dict();
for i in range(0, len(nums)):
x = nums[i]
if target - x in seen:
return [i, seen[target - x]]
seen[x] = i