-
-
Notifications
You must be signed in to change notification settings - Fork 245
[SeongA] WEEK 09 solutions #1914
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
+47
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,29 @@ | ||
public class ListNode { | ||
public var val: Int | ||
public var next: ListNode? | ||
public init(_ val: Int) { | ||
self.val = val | ||
self.next = nil | ||
} | ||
} | ||
|
||
|
||
class Solution { | ||
// Time O(n) | ||
// Space O(1) | ||
func hasCycle(_ head: ListNode?) -> Bool { | ||
var slow: ListNode? = head | ||
var fast: ListNode? = head | ||
|
||
while fast != nil && fast?.next != nil { | ||
slow = slow?.next | ||
fast = fast?.next?.next | ||
|
||
if slow === fast { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
|
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. 나머지 문제들은 난이도가 올라가는 점과 시간이 없는 부분 때문에 확인을 못했는데 생각보다 어려운 문제는 아니었나 싶기도 하네요. 한 번도 접해본 적 없는 언어로 된 코드는 거의 처음 보는데 의도가 명확하게 보였습니다! 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. 다른 분이 읽고 이해하는 데 쉬워서 다행입니다!! |
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,18 @@ | ||
class Solution { | ||
// Time O(log n) | ||
// Space O(1) | ||
func getSum(_ a: Int, _ b: Int) -> Int { | ||
var a = a | ||
var b = b | ||
|
||
while b != 0 { | ||
var sum = a ^ b | ||
var carry = (a & b) << 1 | ||
a = sum | ||
b = carry | ||
} | ||
|
||
return a | ||
} | ||
} | ||
|
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.
오! swift에서는 null이 nil인가 보네요. 신기했어요.
근데 사실 저 이 문제를 제대로 이해하진 못했는데, fast와 slow라는 네이밍을 다들 사용하고 계시더라구요. 혹시 그 이유를 아시나요?
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.
맞아요 swift에는 null을 nil로 써요.
플루이드의 순환 알고리즘(일명 토끼와 거북이 알고리즘)으로 풀었기 때문에. 2칸 이동하는 토끼 = fast, 1칸 이동하는 거북이 = slow라는 네이밍을 쓰고 있어요.