-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b968188
feat: Add solution for LeetCode problem 20
WhiteHyun 5a8bb66
feat: Add solution for LeetCode problem 206
WhiteHyun 18697e0
feat: Add solution for LeetCode problem 21
WhiteHyun 9688594
feat: Add solution for LeetCode problem 141
WhiteHyun 1a625a8
feat: Add solution for LeetCode problem 226
WhiteHyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
||
let left = node.left | ||
let right = node.right | ||
|
||
node.left = invertTree(right) | ||
node.right = invertTree(left) | ||
|
||
return node | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ฌด์๋ฌด์ ํฉ๋๋ค ใ ใ |
||
return false | ||
} | ||
|
||
stack.removeLast() | ||
} | ||
|
||
return stack.isEmpty | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift ์ซ ์ฉ๋๋ฏ ใ ใ