-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sonjh1217] WEEK 08 solutions #1897
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
Merged
Changes from all commits
Commits
Show all changes
4 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,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 | ||
} | ||
} |
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,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 | ||
} | ||
} |
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,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 | ||
} | ||
|
||
} | ||
|
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,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 | ||
} | ||
} |
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.
숫자 단위가 Integer 보다 큰게 되면 어떻게 해주면 될까요 :)
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.
숫자 단위가... 32비트를 넘어갔을때를 말씀하시는 걸까용?