-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sonjh1217] WEEK 09 solutions #1911
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
Conversation
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.
Swift 문법이 풍성하네요! 덕분에 배웠습니다: extension
, Equatable
, Hashable
, ObjectIdentifier
, guard let
, _
, let
, [[Int]]
, [(Int,Int)]
, inout
, repeating
, count
while node != nil { | ||
guard let currentNode = node else { | ||
return false | ||
} | ||
|
||
if visitedNodes.contains(currentNode) { | ||
return true | ||
} | ||
|
||
visitedNodes.insert(currentNode) | ||
node = currentNode.next | ||
} |
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.
이미 nil
Check를 했으니까 Force Unwrapping은 어떨까요?
while node != nil { | |
guard let currentNode = node else { | |
return false | |
} | |
if visitedNodes.contains(currentNode) { | |
return true | |
} | |
visitedNodes.insert(currentNode) | |
node = currentNode.next | |
} | |
while node != nil { | |
if visitedNodes.contains(node!) { | |
return true | |
} | |
visitedNodes.insert(node!) | |
node = node!.next | |
} |
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.
훨씬 깔끔하네요! 감사합니답!
force unwrapping은 지양하려고 하구 있어요 수정하다가 윗 라인이 날아가거나 할 수도 있으니까 만약을 위해서ㅎㅎ
func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) { | ||
var queue = queue | ||
var head = 0 | ||
for (i, j) in queue { | ||
goes[i][j] = true | ||
} | ||
|
||
while head < queue.count { | ||
var (i, j) = queue[head] | ||
head += 1 | ||
|
||
for (di, dj) in directions { | ||
let ni = i + di | ||
let nj = j + dj | ||
if ni >= 0 | ||
&& ni < m | ||
&& nj >= 0 | ||
&& nj < n | ||
&& !goes[ni][nj] | ||
&& heights[ni][nj] >= heights[i][j] { | ||
goes[ni][nj] = true | ||
queue.append((ni, nj)) | ||
} | ||
} | ||
} | ||
} |
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.
재밌는 거 하나 공유하고 갑니다! (저만 재밌음 주의)
최단거리 구하지는 못하지만 Flood-Fill 하기는 좋아요!
func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) { | |
var queue = queue | |
var head = 0 | |
for (i, j) in queue { | |
goes[i][j] = true | |
} | |
while head < queue.count { | |
var (i, j) = queue[head] | |
head += 1 | |
for (di, dj) in directions { | |
let ni = i + di | |
let nj = j + dj | |
if ni >= 0 | |
&& ni < m | |
&& nj >= 0 | |
&& nj < n | |
&& !goes[ni][nj] | |
&& heights[ni][nj] >= heights[i][j] { | |
goes[ni][nj] = true | |
queue.append((ni, nj)) | |
} | |
} | |
} | |
} | |
func depthFirstBreadthSecondSearch(queue: [(Int, Int)], goes: inout [[Bool]]) { | |
var stack = queue | |
for (i, j) in stack { | |
goes[i][j] = true | |
} | |
while !stack.isEmpty { | |
let (i, j) = stack.removeLast() | |
for (di, dj) in directions { | |
let ni = i + di | |
let nj = j + dj | |
if ni >= 0 | |
&& ni < m | |
&& nj >= 0 | |
&& nj < n | |
&& !goes[ni][nj] | |
&& heights[ni][nj] >= heights[i][j] { | |
goes[ni][nj] = true | |
stack.append((ni, nj)) | |
} | |
} | |
} | |
} |
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.
음.. 저도 removeLast도 시도해보긴 했었는데.. 이점을 찾기가 어렵더라구요ㅠ 어떤 점이 좋은가요?
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.
구현하신 dfs
의 재귀함수 대신 stack 반복문으로 구현하실 때 이 구현과 비교해 보시면 DFS도 BFS도 아니면서 일부 중복 검사도 head
도 생략 가능한 혼종임을 아실 수 있으실 거예요. 다만 익숙하지 않으면 혼동을 초래할지도 몰라서 주의해야 하지만 오히려 DFS와 BFS 구현 실수를 줄이는 데 기여하는 변별력을 높일 수 있는 방법이라고도 생각해요 ㅎㅎ
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.
아.. 구현의 이점을 말씀하신 거군요! 공감합니당ㅎㅎ
Swift가 싫은 점도 생겼습니다ㅋㅋ Line 25: Char 19: error: unary operator cannot be separated from its operand in solution.swift
23 | }
24 |
25 | while ! stack.isEmpty {
| `- error: unary operator cannot be separated from its operand
26 | let (i, j) = stack.removeLast()
27 | |
Co-authored-by: yhkee0404 <[email protected]>
와 다 돌려보셨군요 대단하세용!! Swift가 공백에 좀 예민합니당 ㅎㅎ |
protocol oriented 라서 상속 대신 Equatable, Hashable 같은 protocol들을 조합하는 것을 지향하구... 또 type safety를 중요시하는 특징이 있어용! 이렇게 꼼꼼히 리뷰해주시다니 넘 감사합니답!! |
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!