-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from meoooh/main
[김한기] Week 2 solutions
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |