-
-
Notifications
You must be signed in to change notification settings - Fork 203
[doitduri] Week 07 solutions #1476
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
5 commits
Select commit
Hold shift + click to select a range
c2e782b
Solution Reverse linked list
doitduri cfd3c2c
Solution Longest substring without repeating characters
doitduri 1c02235
Solution Number of islands
doitduri 32faa56
Solution Unique paths
doitduri 474f3d5
Solution Set matrix zeroes
doitduri 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
20 changes: 20 additions & 0 deletions
20
longest-substring-without-repeating-characters/doitduri.swift
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,20 @@ | ||
class Solution { | ||
func lengthOfLongestSubstring(_ s: String) -> Int { | ||
var charIndexMap = [Character: Int]() | ||
var maxLength = 0 | ||
var startIndex = 0 | ||
|
||
for (i, char) in Array(s).enumerated() { | ||
if let lastIndex = charIndexMap[char], lastIndex >= startIndex { | ||
startIndex = lastIndex + 1 | ||
} | ||
|
||
let currentLength = i - startIndex + 1 | ||
maxLength = max(maxLength, currentLength) | ||
|
||
charIndexMap[char] = i | ||
} | ||
|
||
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,34 @@ | ||
class Solution { | ||
func numIslands(_ grid: [[Character]]) -> Int { | ||
guard !grid.isEmpty else { return 0 } | ||
|
||
let rows = grid.count | ||
let colums = grid[0].count | ||
var visited = grid | ||
var islandCount = 0 | ||
|
||
func dfs(_ row: Int, _ col: Int) { | ||
guard row >= 0 && row < rows && col >= 0 && col < colums && visited[row][col] == "1" else { | ||
return | ||
} | ||
|
||
visited[row][col] = "0" | ||
|
||
dfs(row - 1, col) | ||
dfs(row + 1, col) | ||
dfs(row, col - 1) | ||
dfs(row, col + 1) | ||
} | ||
|
||
for row in 0..<rows { | ||
for col in 0..<colums { | ||
if visited[row][col] == "1" { | ||
islandCount += 1 | ||
dfs(row, col) | ||
} | ||
} | ||
} | ||
|
||
return islandCount | ||
} | ||
} |
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 { | ||
func reverseList(_ head: ListNode?) -> ListNode? { | ||
var current = head, previous: ListNode? = nil | ||
|
||
while current != nil { | ||
let next = current?.next | ||
current?.next = previous | ||
previous = current | ||
current = next | ||
} | ||
|
||
return previous | ||
} | ||
} |
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,60 @@ | ||
class Solution { | ||
func setZeroes(_ matrix: inout [[Int]]) { | ||
let m = matrix.count | ||
let n = matrix[0].count | ||
|
||
var firstRowHasZero = false | ||
var firstColHasZero = false | ||
|
||
for i in 0..<m { | ||
if matrix[i][0] == 0 { | ||
firstColHasZero = true | ||
break | ||
} | ||
} | ||
|
||
for j in 0..<n { | ||
if matrix[0][j] == 0 { | ||
firstRowHasZero = true | ||
break | ||
} | ||
} | ||
|
||
for i in 1..<m { | ||
for j in 1..<n { | ||
if matrix[i][j] == 0 { | ||
matrix[i][0] = 0 | ||
matrix[0][j] = 0 | ||
} | ||
} | ||
} | ||
|
||
for i in 1..<m { | ||
if matrix[i][0] == 0 { | ||
for j in 1..<n { | ||
matrix[i][j] = 0 | ||
} | ||
} | ||
} | ||
|
||
for j in 1..<n { | ||
if matrix[0][j] == 0 { | ||
for i in 1..<m { | ||
matrix[i][j] = 0 | ||
} | ||
} | ||
} | ||
|
||
if firstRowHasZero { | ||
for j in 0..<n { | ||
matrix[0][j] = 0 | ||
} | ||
} | ||
|
||
if firstColHasZero { | ||
for i in 0..<m { | ||
matrix[i][0] = 0 | ||
} | ||
} | ||
} | ||
} |
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,21 @@ | ||
class Solution { | ||
func uniquePaths(_ m: Int, _ n: Int) -> Int { | ||
var dp = Array(repeating: Array(repeating: 0, count: n), count: m) | ||
|
||
for j in 0..<n { | ||
dp[0][j] = 1 | ||
} | ||
|
||
for i in 0..<m { | ||
dp[i][0] = 1 | ||
} | ||
|
||
for i in 1..<m { | ||
for j in 1..<n { | ||
dp[i][j] = dp[i-1][j] + dp[i][j-1] | ||
} | ||
} | ||
|
||
return dp[m-1][n-1] | ||
} | ||
} | ||
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. 저는 bfs를 사용해서 문제를 풀었는데 DP로도 풀어지는 군요! |
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.
재귀를 사용하면 공간복잡도는 줄어들겠네요!!