Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions linked-list-cycle/delight010.swift
Copy link
Contributor

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라는 네이밍을 다들 사용하고 계시더라구요. 혹시 그 이유를 아시나요?

Copy link
Contributor Author

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라는 네이밍을 쓰고 있어요.

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
}
}

18 changes: 18 additions & 0 deletions sum-of-two-integers/delight010.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나머지 문제들은 난이도가 올라가는 점과 시간이 없는 부분 때문에 확인을 못했는데 생각보다 어려운 문제는 아니었나 싶기도 하네요. 한 번도 접해본 적 없는 언어로 된 코드는 거의 처음 보는데 의도가 명확하게 보였습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른 분이 읽고 이해하는 데 쉬워서 다행입니다!!

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
}
}