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
39 changes: 39 additions & 0 deletions clone-graph/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for a Node.
* public class Node {
* public var val: Int
* public var neighbors: [Node?]
* public init(_ val: Int) {
* self.val = val
* self.neighbors = []
* }
* }
*/

class Solution {
// O(V+E) time / O(V) space
func cloneGraph(_ node: Node?) -> Node? {
guard let node = node else {
return nil
}
var newNodeByVal = [Int: Node]()
return copy(node: node, newNodeByVal: &newNodeByVal)
}

func copy(node: Node, newNodeByVal: inout [Int: Node]) -> Node {
if let node = newNodeByVal[node.val] {
return node
}
var newNode = Node(node.val)
newNodeByVal[node.val] = newNode

for neighbor in node.neighbors {
guard let neighbor = neighbor else {
continue
}
let newNeighbor = copy(node: neighbor, newNodeByVal: &newNodeByVal)
newNode.neighbors.append(newNeighbor)
}
return newNode
}
}
26 changes: 26 additions & 0 deletions longest-repeating-character-replacement/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
// O(n) time / O(n) space
func characterReplacement(_ s: String, _ k: Int) -> Int {
var windowStart = 0
var maxLength = 0
var maxCharacterRepeat = 0
var countByCharacter = [Character: Int]()
var characters = Array(s)

for windowEnd in 0..<characters.count {
countByCharacter[characters[windowEnd], default: 0] += 1
maxCharacterRepeat = max(maxCharacterRepeat, countByCharacter[characters[windowEnd]]!)

var length = windowEnd - windowStart + 1
if length - maxCharacterRepeat <= k {
maxLength = max(maxLength, length)
} else {
while (windowEnd - windowStart + 1) - maxCharacterRepeat > k {
countByCharacter[characters[windowStart]]! -= 1
windowStart += 1
}
}
}
return maxLength
}
}
65 changes: 65 additions & 0 deletions palindromic-substrings/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Solution {
//dynamic programming
// O(n^2) time / O(n^2) space
func countSubstrings1(_ s: String) -> Int {
var count = 0
var characters = Array(s)
var isPalindromes = Array(repeating: Array(repeating: false, count: characters.count), count: characters.count)

for i in 0..<characters.count {
isPalindromes[i][i] = true
count += 1
}

for right in 1..<characters.count {
for left in 0..<right {
if characters[left] == characters[right]
&& (isPalindromes[left+1][right-1] || right-left <= 2) {
isPalindromes[left][right] = true
count += 1
}
}
}

return count
}

//two pointers
// O(n^2) time / O(n) space
func countSubstrings2(_ s: String) -> Int {
var count = 0
var characters = Array(s)
var left: Int
var right: Int

for i in 0..<characters.count{
left = i
right = i

while left >= 0
&& right < characters.count
&& characters[left] == characters[right] {
count += 1

left -= 1
right += 1
}

left = i
right = i+1

while left >= 0
&& right < characters.count
&& characters[left] == characters[right] {
count += 1

left -= 1
right += 1
}
}

return count
}

}

14 changes: 14 additions & 0 deletions reverse-bits/sonjh1217.swift
Copy link
Contributor

Choose a reason for hiding this comment

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

숫자 단위가 Integer 보다 큰게 되면 어떻게 해주면 될까요 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

숫자 단위가... 32비트를 넘어갔을때를 말씀하시는 걸까용?

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
// O(1) time / O(1) space
func reverseBits(_ n: Int) -> Int {
var number = n
var reversed = 0

for _ in 0..<32 {
reversed <<= 1
reversed |= (number & 1)
number >>= 1
}
return reversed
}
}