-
-
Notifications
You must be signed in to change notification settings - Fork 245
[SeongA] WEEK08 Solution #1902
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
[SeongA] WEEK08 Solution #1902
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 } | ||
|
||
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) | ||
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. 오 느낌표 표현이 신기하네요 |
||
} else { | ||
visited[neighbor.val] = Node(neighbor.val) | ||
visited[currentNode.val]!.neighbors.append(visited[neighbor.val]) | ||
queue.append(neighbor) | ||
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. 오.. queue와 visited 를 병행하면서 처음 cloned 한 것에 neighbors 에 cloned 를 append 하는게 인상깊네요ㅎ 👍 |
||
} | ||
} | ||
} | ||
|
||
return firstNode | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
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 | ||
} | ||
} | ||
|
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.
오 이 구문이 엄청 신기하네요
node 에 값이 없으면 바로 return nil 로 한줄로 작성이 되는게 너무 신기해요😲