Skip to content
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
36 changes: 36 additions & 0 deletions graph-valid-tree/sonjh1217.swift
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
Copy link
Contributor

Choose a reason for hiding this comment

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

스위프트에는 증감연산자(++, --)는 없는지 궁금합니다!

Copy link
Contributor Author

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


for node in connectedNodes[currentNode] {
guard !isVisiteds[node] else { continue }

isVisiteds[node] = true
queue.append(node)
}
}

return isVisiteds.allSatisfy { $0 }
}
}
9 changes: 9 additions & 0 deletions missing-number/sonjh1217.swift
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
}
}
97 changes: 97 additions & 0 deletions reorder-list/sonjh1217.swift
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)
}
}