File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed
Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 2929- N: ๋
ธ๋์ ๊ฐ์
3030- dictionary์ ์ฌ๊ท ํธ์ถ ์คํ ๊ณต๊ฐ
3131
32+ ํ์ด ๋ฐฉ๋ฒ:
33+ - ๋ฌธ์ ์์ ์๊ตฌํ๋ ๊ฒ: Deep copy
34+ - Base case: ๋น ๊ทธ๋ํ ์ฒ๋ฆฌ
35+ - ๋์
๋๋ฆฌ ์์ฑ ํ dfs
36+ - ๋ง์ฝ ์ด๋ฏธ ๋ณต์ฌํ ๋
ธ๋๋ผ๋ฉด ํด๋น ๋ณต์ฌ๋ณธ์ ๋ฐํํจ
37+ - ์๋๋ผ๋ฉด, ์๋ก์ด ๋
ธ๋๋ฅผ ์์ฑํ์ฌ ๋์
๋๋ฆฌ์ ์ ์ฅ
38+ - ์ด์ ๋
ธ๋์ ๊ฒฝ์ฐ์๋ dfs()๋ก ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด์ ํ์ฌ ๋
ธ๋์ neighbors ๋ฆฌ์คํธ์ ์ถ๊ฐํจ
39+ - ์ฃผ์ด์ง ๋
ธ๋๋ถํฐ ์ฌ๊ท ์์
40+
3241# Definition for a Node.
3342class Node:
3443 def __init__(self, val = 0, neighbors = None):
3544 self.val = val
3645 self.neighbors = neighbors if neighbors is not None else []
37-
38- ์ฐธ๊ณ ์ฌํญ:
39- - ํผ์ ํ๊ธฐ ์ด๋ ค์์, ๋ฌธ์ ์ ๋ต์ ์ดํดํ๋ ๊ฒ์ ์ง์คํ์ต๋๋ค!
4046"""
4147class Solution :
4248 def cloneGraph (self , node : Optional ['Node' ]) -> Optional ['Node' ]:
49+
50+ # ๋น ๊ทธ๋ํ ์ฒ๋ฆฌ
4351 if not node :
4452 return None
4553
@@ -51,13 +59,11 @@ def dfs(node):
5159
5260 # ์๋ก์ด ๋
ธ๋ ์์ฑ
5361 copy = Node (node .val )
54- dict [node .val ] = copy # dictionary์ ๊ธฐ๋ก
62+ dict [node .val ] = copy
5563
56- # ๊ฐ neighbor์ ๋ํด์๋ ๊ฐ์ ๊ณผ์ ์ํ
57- for neighbor in node .neighbors :
58- copy .neighbors .append (dfs (neighbor ))
64+ for neighbor in node .neighbors : # ์๋ณธ์ ๊ฐ ์ด์์ ๋ํ์ฌ
65+ copy .neighbors .append (dfs (neighbor )) # ๊ทธ ์ด์์ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ด์ ์ถ๊ฐํจ
5966
6067 return copy
6168
6269 return dfs (node )
63-
You canโt perform that action at this time.
0 commit comments