File tree Expand file tree Collapse file tree 5 files changed +143
-0
lines changed Expand file tree Collapse file tree 5 files changed +143
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You canβt perform that action at this time.
0 commit comments