in reply to LeetCode Problem #1 - Two Sum - Improve and/or discuss.
The test is more complex than the code if you want to accept both [0,1] and [1,0] better and easier just rely on the expected sum. Anyway..
sub two_sum_Discipulus { my ($input, $target) = @_; foreach my $index ( 0 .. $#$input ){ ( $_ != $index and $input->[$_] + $input->[$index] == $target +) ? return [$index, $_] + : 0 for 0 .. $#$input; } }
PS added newlines for a minimal readability enhancement
L*
|
|---|