Skip to content
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

[Hyun] Week 2 Solution Explanation #47

Merged
merged 5 commits into from
May 8, 2024
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
23 changes: 23 additions & 0 deletions invert-binary-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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 }
Copy link
Contributor

Choose a reason for hiding this comment

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

Swift ์ซŒ ์ฉŒ๋Š”๋“ฏ ใ…‹ใ…‹


let left = node.left
let right = node.right

node.left = invertTree(right)
node.right = invertTree(left)

return node
}
}
29 changes: 29 additions & 0 deletions linked-list-cycle/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
20 changes: 20 additions & 0 deletions reverse-linked-list/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions valid-parentheses/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

๋ฌด์‹œ๋ฌด์‹œ ํ•ฉ๋‹ˆ๋‹ค ใ…‹ใ…‹

return false
}

stack.removeLast()
}

return stack.isEmpty
}
}