-
-
Notifications
You must be signed in to change notification settings - Fork 195
[김한기] Week 2 solutions #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent가 깨진것 같네요...!