lytlang/test/fixtures/example.ex

24 lines
547 B
Elixir
Raw Permalink Normal View History

2019-05-11 13:01:39 -05:00
defmodule Leetcode do
@doc """
LeetCode #1: two_sum
TODO: store a map of complements (differences) as iterating for O(n)
"""
@spec two_sum(list, int, int) :: any
def two_sum(_, _, i \\ 0)
def two_sum(nums, target, current_index \\ 0)
def two_sum([], _target, _i), do: nil
def two_sum([cur | rest], target, current_index) do
case Enum.find_index(rest, fn x -> cur + x == target end) do
nil -> two_sum(rest, target, current_index + 1)
i -> [current_index, 1 + current_index + i]
end
end
# ...
end