diff --git a/invert-binary-tree/WhiteHyun.swift b/invert-binary-tree/WhiteHyun.swift new file mode 100644 index 000000000..4f856ed1e --- /dev/null +++ b/invert-binary-tree/WhiteHyun.swift @@ -0,0 +1,23 @@ +// +// 226. Invert Binary Tree.swift +// https://leetcode.com/problems/invert-binary-tree/description/ +// Algorithm +// +// Created by 홍승현 on 2024/05/04. +// + +import Foundation + +final class LeetCode226 { + func invertTree(_ node: TreeNode?) -> TreeNode? { + guard let node else { return nil } + + let left = node.left + let right = node.right + + node.left = invertTree(right) + node.right = invertTree(left) + + return node + } +} diff --git a/linked-list-cycle/WhiteHyun.swift b/linked-list-cycle/WhiteHyun.swift new file mode 100644 index 000000000..6f205443b --- /dev/null +++ b/linked-list-cycle/WhiteHyun.swift @@ -0,0 +1,29 @@ +// +// 141. Linked List Cycle.swift +// https://leetcode.com/problems/linked-list-cycle/description/ +// Algorithm +// +// Created by 홍승현 on 2024/05/04. +// + +import Foundation + +final class LeetCode141 { + func hasCycle(_ head: ListNode?) -> Bool { + guard head != nil, head?.next != nil + else { + return false + } + + var tortoise = head + var hare = head?.next + + while hare != nil, hare?.next != nil { + if tortoise === hare { return true } + tortoise = tortoise?.next + hare = hare?.next?.next + } + + return false + } +} diff --git a/merge-two-sorted-lists/WhiteHyun.swift b/merge-two-sorted-lists/WhiteHyun.swift new file mode 100644 index 000000000..0ac929845 --- /dev/null +++ b/merge-two-sorted-lists/WhiteHyun.swift @@ -0,0 +1,34 @@ +// +// 21. Merge Two Sorted Lists.swift +// https://leetcode.com/problems/merge-two-sorted-lists/description/ +// Algorithm +// +// Created by 홍승현 on 2024/05/04. +// + +import Foundation + +final class LeetCode21 { + func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { + let dummy: ListNode? = .init(0) + var currentNode: ListNode? = dummy + var l1 = list1 + var l2 = list2 + + while l1 != nil, l2 != nil { + if l1!.val < l2!.val { + currentNode?.next = l1 + l1 = l1?.next + } else { + currentNode?.next = l2 + l2 = l2?.next + } + + currentNode = currentNode?.next + } + + currentNode?.next = l1 ?? l2 + + return dummy?.next + } +} diff --git a/reverse-linked-list/WhiteHyun.swift b/reverse-linked-list/WhiteHyun.swift new file mode 100644 index 000000000..422746a11 --- /dev/null +++ b/reverse-linked-list/WhiteHyun.swift @@ -0,0 +1,20 @@ +// +// 206. Reverse Linked List.swift +// https://leetcode.com/problems/reverse-linked-list/description/ +// Algorithm +// +// Created by 홍승현 on 2024/05/04. +// + +import Foundation + +final class LeetCode206 { + func reverseList(_ node: ListNode?, _ prev: ListNode? = nil) -> ListNode? { + guard let node else { return prev } + + let next = node.next + node.next = prev + + return reverseList(next, node) + } +} diff --git a/valid-parentheses/WhiteHyun.swift b/valid-parentheses/WhiteHyun.swift new file mode 100644 index 000000000..fdc10d608 --- /dev/null +++ b/valid-parentheses/WhiteHyun.swift @@ -0,0 +1,37 @@ +// +// 20. Valid Parentheses.swift +// https://leetcode.com/problems/valid-parentheses/description/ +// Algorithm +// +// Created by 홍승현 on 2024/05/04. +// + +import Foundation + +final class LeetCode20 { + func isValid(_ s: String) -> Bool { + var stack: [Character] = [] + + for character in s { + if "([{".contains(character) { + stack.append(character) + continue + } + + // `([{`가 아닌 닫힌 괄호가 들어왔는데 stack이 비어있으면 안 됨. + if stack.isEmpty { return false } + + // 아스키 값의 차를 활용하여 1~2사이의 차이가 난다면 알맞은 쌍. + // 하지만, 각 괄호의 아스키 값이 3 이상 나는 경우는 서로 맞지 않음. + // Swift에서는 asciiValue 프로퍼티의 타입이 `UInt8` 이기 때문에 음수가 없어서 가능한 비교! + // `&-` 는 오버플로우 연산으로, 만약 오버플로우가 난다면 무시함. + if character.asciiValue! &- stack.last!.asciiValue! > 2 { + return false + } + + stack.removeLast() + } + + return stack.isEmpty + } +}