-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sonjh1217] WEEK 11 Solutions #1927
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
Merged
+142
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,36 @@ | ||
import Foundation | ||
|
||
class Solution { | ||
// O(n) time / O(n) space | ||
func validTree(_ n: Int, _ edges: [[Int]]) -> Bool { | ||
guard edges.count == n - 1 else { | ||
return false | ||
} | ||
|
||
var connectedNodes = Array(repeating: [Int](), count: n) | ||
|
||
for edge in edges { | ||
connectedNodes[edge[0]].append(edge[1]) | ||
connectedNodes[edge[1]].append(edge[0]) | ||
} | ||
|
||
var isVisiteds = Array(repeating: false, count: n) | ||
var head = 0 | ||
var queue = [0] | ||
isVisiteds[0] = true | ||
|
||
while head < queue.count { | ||
let currentNode = queue[head] | ||
head += 1 | ||
|
||
for node in connectedNodes[currentNode] { | ||
guard !isVisiteds[node] else { continue } | ||
|
||
isVisiteds[node] = true | ||
queue.append(node) | ||
} | ||
} | ||
|
||
return isVisiteds.allSatisfy { $0 } | ||
} | ||
} |
This file contains hidden or 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,9 @@ | ||
class Solution { | ||
// O(n) time / O(1) space | ||
func missingNumber(_ nums: [Int]) -> Int { | ||
let count = nums.count | ||
var expectedSum = count * (count + 1) / 2 | ||
let actualSum = nums.reduce(0, +) | ||
return expectedSum - actualSum | ||
} | ||
} |
This file contains hidden or 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,97 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* public var val: Int | ||
* public var next: ListNode? | ||
* public init() { self.val = 0; self.next = nil; } | ||
* public init(_ val: Int) { self.val = val; self.next = nil; } | ||
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
// O(n) time / O(n) space | ||
func reorderListUsingArray(_ head: ListNode?) { | ||
var nodes = [ListNode]() | ||
var node = head | ||
while node != nil { | ||
nodes.append(node!) | ||
node = node!.next | ||
} | ||
|
||
node = head | ||
var fromFirstIndex = 1 | ||
var fromLastIndex = nodes.count - 1 | ||
|
||
while fromFirstIndex <= fromLastIndex { | ||
guard node != nil, nodes.last != nil else { | ||
break | ||
} | ||
node!.next = nodes.removeLast() | ||
node = node!.next | ||
|
||
guard fromFirstIndex < fromLastIndex else { | ||
break | ||
} | ||
|
||
fromLastIndex -= 1 | ||
node!.next = nodes[fromFirstIndex] | ||
fromFirstIndex += 1 | ||
node = node!.next | ||
} | ||
|
||
node?.next = nil | ||
} | ||
|
||
// O(n) time / O(1) space | ||
func reorderList(_ head: ListNode?) { | ||
func findMiddle(_ head: ListNode?) -> ListNode? { | ||
var slow = head | ||
var fast = head | ||
|
||
while fast?.next?.next != nil { | ||
slow = slow?.next | ||
fast = fast?.next?.next | ||
} | ||
|
||
return slow | ||
} | ||
|
||
var mid = findMiddle(head) | ||
|
||
func reverseList(_ head: ListNode?) -> ListNode? { | ||
var prev: ListNode? = nil | ||
var curr: ListNode? = head | ||
var next: ListNode? = curr?.next | ||
|
||
while curr != nil { | ||
curr?.next = prev | ||
|
||
prev = curr | ||
curr = next | ||
next = next?.next | ||
} | ||
|
||
return prev | ||
} | ||
|
||
let reversedToMid = reverseList(mid?.next) | ||
mid?.next = nil | ||
|
||
func mergeList(first: ListNode?, second: ListNode?) { | ||
var first = first | ||
var second = second | ||
|
||
while first != nil { | ||
let firstTmp = first?.next | ||
first?.next = second | ||
let secondTmp = second?.next | ||
second?.next = firstTmp | ||
|
||
first = firstTmp | ||
second = secondTmp | ||
} | ||
} | ||
|
||
mergeList(first: head, second: reversedToMid) | ||
} | ||
} |
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.
스위프트에는 증감연산자(++, --)는 없는지 궁금합니다!
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.
넵 없습니다 찾아보니까 엄청 초기에 제거가 되었네용! https://github.com/swiftlang/swift-evolution/blob/main/proposals/0004-remove-pre-post-inc-decrement.md