-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Arthur] Week 3 #783
Merged
Merged
[Arthur] Week 3 #783
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 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 @@ | ||
// time complexity: O(n) | ||
// space complexity: O(1) | ||
// prefix와 postfix를 이용하여 계산 | ||
// 예를 들어, [1, 2, 3, 4] 일 때, | ||
// prefix는 [1, 1, 2, 6] 이고 postfix는 [24, 12, 4, 1] 이다. | ||
// 그리고 서로 곱하면 [24, 12, 8, 6] 이 된다. | ||
func productExceptSelf(nums []int) []int { | ||
res := make([]int, len(nums)) | ||
for i := range res { | ||
res[i] = 1 | ||
} | ||
|
||
prefix := 1 | ||
for i := 0; i < len(nums); i++ { | ||
res[i] = prefix | ||
prefix *= nums[i] | ||
} | ||
|
||
postfix := 1 | ||
for i := len(nums) - 1; i >= 0; i-- { | ||
res[i] *= postfix | ||
postfix *= nums[i] | ||
} | ||
|
||
return res | ||
} |
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,6 @@ | ||
// Time Complexity: O(1) | ||
// Space Complexity: O(1) | ||
func reverseBits(num uint32) uint32 { | ||
reversedBits := bits.Reverse32(num) | ||
return reversedBits | ||
} |
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,15 @@ | ||
// Time: O(n) | ||
// Space: O(n) | ||
func twoSum(nums []int, target int) []int { | ||
m := make(map[int]int) | ||
|
||
// O(n) | ||
for i, num := range nums { | ||
// O(1) | ||
if j, ok := m[target-num]; ok && j != i { // target = num2 + num1 -> num2 = target - num1 을 이용하여 두 수를 찾는다. | ||
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. 오 정말 간결하군요! |
||
return []int{j, i} | ||
} | ||
m[num] = i // 없다면 현재 수를 키로 하여 인덱스를 저장한다. | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
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.
한 수 배워갑니다 ㅎㅎㅎ
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.
내부 구현이 궁금해서 좀 찾아봤는데, 궁금하시면 한 번 구경해보시죠 ㅎㅎ
https://github.com/golang/go/blob/master/src/math/bits/bits.go
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.
오 이런 함수가 있었군요 ㅎㅎ 덕분에 알아갑니다!