Skip to content

Commit 1763d35

Browse files
author
Your Name
committed
day 17
1 parent 996a984 commit 1763d35

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

138CopyListwithRandomPointer.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, x, next=None, random=None):
5+
self.val = int(x)
6+
self.next = next
7+
self.random = random
8+
9+
10+
class Solution(object):
11+
def copyRandomList(self, head):
12+
"""
13+
:type head: Node
14+
:rtype: Node
15+
[[3,null],[2,0],[1,null]]
16+
"""
17+
18+
cacheMap = {}
19+
20+
curr = head
21+
newCurr = None
22+
last = None
23+
newHead = None
24+
while curr:
25+
val = curr.val
26+
newCurr = Node(val)
27+
if last:
28+
last.next = newCurr
29+
else:
30+
newHead = newCurr
31+
cacheMap[curr] = newCurr
32+
last = newCurr
33+
curr = curr.next
34+
35+
curr = head
36+
newCurr = newHead
37+
while curr:
38+
random = curr.random
39+
mirrorRandom = None
40+
if random:
41+
mirrorRandom = cacheMap[random]
42+
newCurr.random = mirrorRandom
43+
44+
curr = curr.next
45+
newCurr = newCurr.next
46+
return newHead
47+
48+
49+
a = Node(3)
50+
b = Node(2)
51+
c = Node(1)
52+
53+
a.next = b
54+
b.next = c
55+
b.random = a
56+
57+
sol = Solution()
58+
res = sol.copyRandomList(a)
59+
print(res)
60+

207.CourseSchedule.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def canFinish(self, numCourses, prerequisites):
3+
"""
4+
:type numCourses: int
5+
:type prerequisites: List[List[int]]
6+
:rtype: bool
7+
[[1,0],[0,1]]
8+
"""
9+

0 commit comments

Comments
 (0)