!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] @doc LeetCode #2: add_two_numbers fn add_two_numbers _:list _:list _=[] i=0 :: [] _ l m, _ [] l m -> finish_add_two_numbers l m [n1 | r1] [n2 | r2] l carry add_two_numbers r1 r2 [rem (carry + n1 + n2) 10) | l] (div n1 + n2 10) fn finish_add_two_numbers l:list m:int -> m == 0 ? l : [m | l] @doc LeetCode #3: longest non-repeating susbtring TODO: better utility function names? fn longest_non_repeating_substring s s |> String.codepoints |> Enum.reduce {0 0 0 %{}} &ln3_reducer/2 |> elem 0 fn ln3_new_least least map char Map.has_key? map char ? max least ((Map.get map char) + 1) : least fn ln3_reducer char {mx i least map} least = ln3_new_least least map char {(max mx (i - least + 1) (i + 1) least (Map.put map char i)} @doc LeetCode #4: median of sorted arrays TODO: solution should me O(log(m+n)) TODO: finish an actual solution? fn median_of_sorted_arrays _ _ -> 3 @doc LeetCode #65: Valid Number @valid_decimal_number_regex ~r/^\s*(?:\+|-)?\d+(?:\.(?=\d))?\d*(?:e(?=[\d\+-]))?(?:\+|-)?\d*(?:\.(?=\d))?\s*$/ fn valid_decimal_number s -> Regex.match? @valid_decimal_number_regex s