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
34 changes: 34 additions & 0 deletions clone-graph/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
// Time O(V+E)
// Space O(V)
func cloneGraph(_ node: Node?) -> Node? {
guard let node = 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.

오 이 구문이 엄청 신기하네요
node 에 값이 없으면 바로 return nil 로 한줄로 작성이 되는게 너무 신기해요😲


var visited: [Int: Node] = [:]
var queue: [Node] = []

let firstNode = Node(node.val)
visited[node.val] = firstNode

queue.append(node)

while !queue.isEmpty {
let currentNode = queue.removeFirst()

for neighbor in currentNode.neighbors {
guard let neighbor = neighbor else { continue }

if let clonedNeighbor = visited[neighbor.val] {
visited[currentNode.val]!.neighbors.append(clonedNeighbor)
Copy link
Contributor

Choose a reason for hiding this comment

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

오 느낌표 표현이 신기하네요
seongA님 덕에 swift 문법에 신기한걸 많이 배우고 있어요ㅎㅎ 값이 있다고 치고 optional 에서 값을 빼버리다니
마치 css 에서 important (!) 같은 느낌이네요ㅎㅎ

} else {
visited[neighbor.val] = Node(neighbor.val)
visited[currentNode.val]!.neighbors.append(visited[neighbor.val])
queue.append(neighbor)
Copy link
Contributor

Choose a reason for hiding this comment

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

오.. queue와 visited 를 병행하면서 처음 cloned 한 것에 neighbors 에 cloned 를 append 하는게 인상깊네요ㅎ 👍

}
}
}

return firstNode
}
}

20 changes: 20 additions & 0 deletions longest-common-subsequence/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
// Time (MN)
// Space (MN)
func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int {
var dp = Array(repeating: Array(repeating: 0, count: text2.count + 1),
count: text1.count + 1)
for (i, char1) in text1.enumerated() {
for (j, char2) in text2.enumerated() {
if char1 == char2 {
dp[i + 1][j + 1] = dp[i][j] + 1
} else {
dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])
}
}
}

return dp[text1.count][text2.count]
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution {
// Time complexity O(N)
// Space complexity O(min(m,n))
// Space complexity O(N)
func lengthOfLongestSubstring(_ s: String) -> Int {
if s.isEmpty {
return 0
Expand Down
35 changes: 35 additions & 0 deletions palindromic-substrings/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
// Time O(n^2)
// Space O(n)
func countSubstrings(_ s: String) -> Int {
let charArray = Array(s)
var count = 0

for i in 0..<charArray.count {
count += countPalindrome(charArray, i, i)
count += countPalindrome(charArray, i, i + 1)
}

return count
}

private func countPalindrome(_ s: [Character], _ left: Int, _ right: Int) -> Int {
var left = left
var right = right
var count = 0

while left >= 0 && right < s.count {
if s[left] == s[right] {
count += 1
} else {
break
}

left -= 1
right += 1
}

return count
}
}

18 changes: 18 additions & 0 deletions reverse-bits/delight010.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
// Time O(1)
// Space O(1)
func reverseBits(_ n: Int) -> Int {
var number = n
var result = 0

for i in 0..<32 {
let bit = number & 1
result = result << 1
result = result | bit
number = number >> 1
}

return result
}
}