leetcode: solve two sum problem in elixir
This commit is contained in:
parent
e0cc88e3ff
commit
bb5eb9323f
8 changed files with 98 additions and 0 deletions
4
leetcode/0001-two-sum/elixir/.formatter.exs
Normal file
4
leetcode/0001-two-sum/elixir/.formatter.exs
Normal file
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
26
leetcode/0001-two-sum/elixir/.gitignore
vendored
Normal file
26
leetcode/0001-two-sum/elixir/.gitignore
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
# The directory Mix will write compiled artifacts to.
|
||||
/_build/
|
||||
|
||||
# If you run "mix test --cover", coverage assets end up here.
|
||||
/cover/
|
||||
|
||||
# The directory Mix downloads your dependencies sources to.
|
||||
/deps/
|
||||
|
||||
# Where third-party dependencies like ExDoc output generated docs.
|
||||
/doc/
|
||||
|
||||
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||
/.fetch
|
||||
|
||||
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||
erl_crash.dump
|
||||
|
||||
# Also ignore archive artifacts (built via "mix archive.build").
|
||||
*.ez
|
||||
|
||||
# Ignore package tarball (built via "mix hex.build").
|
||||
two_sum-*.tar
|
||||
|
||||
# Temporary files, for example, from tests.
|
||||
/tmp/
|
23
leetcode/0001-two-sum/elixir/lib/two_sum.ex
Normal file
23
leetcode/0001-two-sum/elixir/lib/two_sum.ex
Normal file
|
@ -0,0 +1,23 @@
|
|||
defmodule Solution do
|
||||
@spec two_sum(nums :: [integer], target :: integer) :: [integer]
|
||||
def two_sum(nums, target) do
|
||||
process_num(nums, target)
|
||||
end
|
||||
|
||||
@spec process_num(
|
||||
nums :: [integer],
|
||||
target :: integer,
|
||||
head_idx :: integer,
|
||||
indexes :: %{integer => integer}
|
||||
) :: [integer]
|
||||
defp process_num([head | tail], target, head_idx \\ 0, indexes \\ %{}) do
|
||||
first_idx = indexes[target - head]
|
||||
|
||||
if first_idx != nil do
|
||||
[first_idx, head_idx]
|
||||
else
|
||||
indexes = Map.put(indexes, head, head_idx)
|
||||
process_num(tail, target, head_idx + 1, indexes)
|
||||
end
|
||||
end
|
||||
end
|
28
leetcode/0001-two-sum/elixir/mix.exs
Normal file
28
leetcode/0001-two-sum/elixir/mix.exs
Normal file
|
@ -0,0 +1,28 @@
|
|||
defmodule TwoSum.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :two_sum,
|
||||
version: "0.1.0",
|
||||
elixir: "~> 1.16",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps()
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help compile.app" to learn about applications.
|
||||
def application do
|
||||
[
|
||||
extra_applications: [:logger]
|
||||
]
|
||||
end
|
||||
|
||||
# Run "mix help deps" to learn about dependencies.
|
||||
defp deps do
|
||||
[
|
||||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
]
|
||||
end
|
||||
end
|
1
leetcode/0001-two-sum/elixir/test/test_helper.exs
Normal file
1
leetcode/0001-two-sum/elixir/test/test_helper.exs
Normal file
|
@ -0,0 +1 @@
|
|||
ExUnit.start()
|
16
leetcode/0001-two-sum/elixir/test/two_sum_test.exs
Normal file
16
leetcode/0001-two-sum/elixir/test/two_sum_test.exs
Normal file
|
@ -0,0 +1,16 @@
|
|||
defmodule SolutionTest do
|
||||
use ExUnit.Case
|
||||
doctest Solution
|
||||
|
||||
test "example 1" do
|
||||
assert Solution.two_sum([2, 7, 11, 15], 9) == [0, 1]
|
||||
end
|
||||
|
||||
test "example 2" do
|
||||
assert Solution.two_sum([3, 2, 4], 6) == [1, 2]
|
||||
end
|
||||
|
||||
test "example 3" do
|
||||
assert Solution.two_sum([3, 3], 6) == [0, 1]
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue