Skip to content

Commit

Permalink
Merge pull request #64 from meoooh/main
Browse files Browse the repository at this point in the history
[김한기] Week 2 solutions
  • Loading branch information
leokim0922 authored May 12, 2024
2 parents b43de0f + 2a8ceae commit 5e2cd35
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
37 changes: 37 additions & 0 deletions invert-binary-tree/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end

defmodule Solution do
@spec invert_tree(root :: TreeNode.t | nil) :: TreeNode.t | nil
def invert_tree(nil), do: nil

def invert_tree(root) do
invert(root, root.left, root.right)
end

defp invert(parent, nil, nil), do: parent

defp invert(parent, left, nil) do
%TreeNode{parent | left: nil, right: invert(left, left.left, left.right)}
end

defp invert(parent, nil, right) do
%TreeNode{parent | left: invert(right, right.left, right.right), right: nil}
end

defp invert(parent, left, right) do
tmp = left

%TreeNode{parent |
left: invert(right, right.left, right.right),
right: invert(tmp, tmp.left, tmp.right)}
end
end
26 changes: 26 additions & 0 deletions linked-list-cycle/han.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end

# @param {ListNode} head
# @return {Boolean}
def hasCycle(head)
return false unless head && head.next

slow = head
fast = head.next

loop do
break true if slow == fast

slow = slow.next
fast = fast&.next&.next

break false unless fast
end
end
24 changes: 24 additions & 0 deletions merge-two-sorted-lists/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for singly-linked list.
#
# defmodule ListNode do
# @type t :: %__MODULE__{
# val: integer,
# next: ListNode.t() | nil
# }
# defstruct val: 0, next: nil
# end

defmodule Solution do
@spec merge_two_lists(list1 :: ListNode.t | nil, list2 :: ListNode.t | nil) :: ListNode.t | nil
def merge_two_lists(nil, nil), do: nil
def merge_two_lists(nil, list2), do: list2
def merge_two_lists(list1, nil), do: list1

def merge_two_lists(list1, list2) when list1.val >= list2.val do
%ListNode{list2 | next: merge_two_lists(list1, list2.next)}
end

def merge_two_lists(list1, list2) do
%ListNode{list1 | next: merge_two_lists(list1.next, list2)}
end
end
25 changes: 25 additions & 0 deletions reverse-linked-list/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Definition for singly-linked list.
#
# defmodule ListNode do
# @type t :: %__MODULE__{
# val: integer,
# next: ListNode.t() | nil
# }
# defstruct val: 0, next: nil
# end

defmodule Solution do
def reverse_list(head) when is_nil(head), do: nil

@spec reverse_list(head :: ListNode.t | nil) :: ListNode.t | nil
def reverse_list(head, arr \\ []) do
new_arr = List.insert_at(arr, -1, head.val)
if head.next do
reverse_list(head.next, new_arr)
else
Enum.reduce(tl(new_arr), %ListNode{val: hd(new_arr), next: nil}, fn x, acc ->
%ListNode{val: x, next: acc}
end)
end
end
end
21 changes: 21 additions & 0 deletions valid-parentheses/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Solution do
def is_valid(s) when byte_size(s) > 1 do
brackets = %{")" => "(", "}" => "{", "]" => "["}
opens = ["[", "{", "("]

result = Enum.reduce_while(String.graphemes(s), [], fn x, acc ->
cond do
x in opens ->
{:cont, List.insert_at(acc, -1, x)}
brackets[x] == List.last(acc) ->
{:cont, List.delete_at(acc, -1)}
true ->
{:halt, List.insert_at(acc, -1, x)}
end
end)

length(result) < 1
end

def is_valid(_), do: false
end

0 comments on commit 5e2cd35

Please sign in to comment.