13 lines
339 B
Text
13 lines
339 B
Text
!mod Leetcode
|
|
|
|
@doc
|
|
LeetCode #1: two_sum
|
|
|
|
TODO: store a map of complements (differences) as iterating for O(n)
|
|
|
|
fn two_sum _:list _:int i=0 ::
|
|
[] _ _ -> nil
|
|
[cur | rest] target current_index
|
|
Enum.find_index rest, fn x -> cur + x == target ::
|
|
nil -> two_sum rest target current_index
|
|
i -> [current_index, 1 + current_index + i]
|