Skip to content

Commit e01b39c

Browse files
authored
Merge pull request DaleStudy#47 from WhiteHyun/main
[Hyun] Week 2 Solution Explanation
2 parents bdb5680 + 1a625a8 commit e01b39c

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

β€Žinvert-binary-tree/WhiteHyun.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// 226. Invert Binary Tree.swift
3+
// https://leetcode.com/problems/invert-binary-tree/description/
4+
// Algorithm
5+
//
6+
// Created by ν™μŠΉν˜„ on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode226 {
12+
func invertTree(_ node: TreeNode?) -> TreeNode? {
13+
guard let node else { return nil }
14+
15+
let left = node.left
16+
let right = node.right
17+
18+
node.left = invertTree(right)
19+
node.right = invertTree(left)
20+
21+
return node
22+
}
23+
}

β€Žlinked-list-cycle/WhiteHyun.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// 141. Linked List Cycle.swift
3+
// https://leetcode.com/problems/linked-list-cycle/description/
4+
// Algorithm
5+
//
6+
// Created by ν™μŠΉν˜„ on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode141 {
12+
func hasCycle(_ head: ListNode?) -> Bool {
13+
guard head != nil, head?.next != nil
14+
else {
15+
return false
16+
}
17+
18+
var tortoise = head
19+
var hare = head?.next
20+
21+
while hare != nil, hare?.next != nil {
22+
if tortoise === hare { return true }
23+
tortoise = tortoise?.next
24+
hare = hare?.next?.next
25+
}
26+
27+
return false
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 21. Merge Two Sorted Lists.swift
3+
// https://leetcode.com/problems/merge-two-sorted-lists/description/
4+
// Algorithm
5+
//
6+
// Created by ν™μŠΉν˜„ on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode21 {
12+
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
13+
let dummy: ListNode? = .init(0)
14+
var currentNode: ListNode? = dummy
15+
var l1 = list1
16+
var l2 = list2
17+
18+
while l1 != nil, l2 != nil {
19+
if l1!.val < l2!.val {
20+
currentNode?.next = l1
21+
l1 = l1?.next
22+
} else {
23+
currentNode?.next = l2
24+
l2 = l2?.next
25+
}
26+
27+
currentNode = currentNode?.next
28+
}
29+
30+
currentNode?.next = l1 ?? l2
31+
32+
return dummy?.next
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// 206. Reverse Linked List.swift
3+
// https://leetcode.com/problems/reverse-linked-list/description/
4+
// Algorithm
5+
//
6+
// Created by ν™μŠΉν˜„ on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode206 {
12+
func reverseList(_ node: ListNode?, _ prev: ListNode? = nil) -> ListNode? {
13+
guard let node else { return prev }
14+
15+
let next = node.next
16+
node.next = prev
17+
18+
return reverseList(next, node)
19+
}
20+
}

β€Žvalid-parentheses/WhiteHyun.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// 20. Valid Parentheses.swift
3+
// https://leetcode.com/problems/valid-parentheses/description/
4+
// Algorithm
5+
//
6+
// Created by ν™μŠΉν˜„ on 2024/05/04.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode20 {
12+
func isValid(_ s: String) -> Bool {
13+
var stack: [Character] = []
14+
15+
for character in s {
16+
if "([{".contains(character) {
17+
stack.append(character)
18+
continue
19+
}
20+
21+
// `([{`κ°€ μ•„λ‹Œ λ‹«νžŒ κ΄„ν˜Έκ°€ λ“€μ–΄μ™”λŠ”λ° stack이 λΉ„μ–΄μžˆμœΌλ©΄ μ•ˆ 됨.
22+
if stack.isEmpty { return false }
23+
24+
// μ•„μŠ€ν‚€ κ°’μ˜ μ°¨λ₯Ό ν™œμš©ν•˜μ—¬ 1~2μ‚¬μ΄μ˜ 차이가 λ‚œλ‹€λ©΄ μ•Œλ§žμ€ 쌍.
25+
// ν•˜μ§€λ§Œ, 각 κ΄„ν˜Έμ˜ μ•„μŠ€ν‚€ 값이 3 이상 λ‚˜λŠ” κ²½μš°λŠ” μ„œλ‘œ λ§žμ§€ μ•ŠμŒ.
26+
// Swiftμ—μ„œλŠ” asciiValue ν”„λ‘œνΌν‹°μ˜ νƒ€μž…μ΄ `UInt8` 이기 λ•Œλ¬Έμ— μŒμˆ˜κ°€ μ—†μ–΄μ„œ κ°€λŠ₯ν•œ 비ꡐ!
27+
// `&-` λŠ” μ˜€λ²„ν”Œλ‘œμš° μ—°μ‚°μœΌλ‘œ, λ§Œμ•½ μ˜€λ²„ν”Œλ‘œμš°κ°€ λ‚œλ‹€λ©΄ λ¬΄μ‹œν•¨.
28+
if character.asciiValue! &- stack.last!.asciiValue! > 2 {
29+
return false
30+
}
31+
32+
stack.removeLast()
33+
}
34+
35+
return stack.isEmpty
36+
}
37+
}

0 commit comments

Comments
Β (0)