-
-
Notifications
You must be signed in to change notification settings - Fork 195
[김한기] - Week 4 solutions #87
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,8 @@ | ||
defmodule Solution do | ||
@spec count_bits(n :: integer) :: [integer] | ||
def count_bits(n) do | ||
Enum.map(0..n, fn i -> | ||
Integer.digits(i, 2) |> Enum.count(&(&1 != 0)) | ||
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,31 @@ | ||
defmodule Solution do | ||
@spec group_anagrams(strs :: [String.t]) :: [[String.t]] | ||
def group_anagrams(strs) do | ||
Enum.group_by(strs, &(String.codepoints(&1) |> Enum.sort)) |> Map.values | ||
end | ||
end | ||
|
||
# 최초엔 아래 코드로 시도했으나 strs.length가 커지면 시간초과... | ||
# | ||
# defmodule Solution do | ||
# @spec group_anagrams(strs :: [String.t]) :: [[String.t]] | ||
# def group_anagrams(strs) when length(strs) < 2, do: [strs] | ||
# def group_anagrams([], res), do: res | ||
|
||
# def group_anagrams([head|tail], res \\ []) do | ||
# split_sort_head = split_sort(head) | ||
# anagrams = Enum.reduce(tail, [head], fn x, acc -> | ||
# if split_sort(x) == split_sort_head do | ||
# acc ++ [x] | ||
# else | ||
# acc | ||
# end | ||
# end) | ||
|
||
# group_anagrams(tail -- anagrams, [anagrams|res]) | ||
# end | ||
|
||
# defp split_sort(str) do | ||
# String.graphemes(str) |> Enum.sort_by(&(&1), :asc) | ||
# 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,6 @@ | ||
defmodule Solution do | ||
@spec missing_number(nums :: [integer]) :: integer | ||
def missing_number(nums) do | ||
(0..length(nums) |> Enum.to_list) -- nums |> Enum.at(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 신기한 연산자가 많네요. 언어별 특징이 보이는 것 같아 보는 맛이 있네요 ㅎㅎ |
||
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,6 @@ | ||
defmodule Solution do | ||
@spec hamming_weight(n :: integer) :: integer | ||
def hamming_weight(n) do | ||
Integer.to_string(n, 2) |> String.graphemes |> Enum.count(& &1 != "0") | ||
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,6 @@ | ||
# @param {Integer} n, a positive integer | ||
# @return {Integer} | ||
def reverse_bits(n) | ||
# 이유는 모르겠으나 문제 설명과는 달리 leetcode 루비 테스트코드에선 n이 10진수 Integer로 들어오네요. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 언어에서도 마찬가지에요. 문제가 좀 오해가 소지가 있게 설명이 되어 있는 것 같아요 😅 |
||
n.to_s(2).rjust(32,'0').reverse.to_i(2) | ||
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.
시간 복잡도가$O(n * log n)$ 이 될 것 같은데, 빨리 제출하셨으니 $O(n)$ 솔루션도 고민해보시면 좋을 것 같습니다.
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.
제가 사실 시간복잡도 공간복잡도 개념이 익숙치 않은데 공부해야겠네요 ^^;
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.
코딩 인터뷰에서 시공간 복잡도 분석이 필요한 걸로 알고 있는데 맞을까요?