743. Network Delay Time
Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
Output: 2
class Solution(object):
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
# general idea is using BFS to search from the starting node, and then the around circle node, and last, return the max path length to cover all the node path.
edge=[[-1 for i in range(101)] for j in range(101)]
dist=[sys.maxint for i in range(N+1)]
dist[K]=0 # mark the K node dist to 0
#N node number index is not range in (1,N), so we should cover all the case in range[1,100]
for t in times:
edge[t[0]][t[1]]=t[2]
q=[K]
while q:
visited=set()
l=len(q)
for i in range(l):
u=q[0] #must use queue here
del q[0]
for v in range(1,101):
if edge[u][v]!=-1 and dist[u]+edge[u][v]<dist[v]:
if v not in visited:
q.append(v)
visited.add(v)
#update the shortest path length
dist[v]=dist[u]+edge[u][v]
res=max(dist[1:])
if res==sys.maxint:
return -1
return res
133. clone graph
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, neighbors):
self.val = val
self.neighbors = neighbors
"""
class Solution(object):
def cloneGraph(self, node):
"""
:type node: Node
:rtype: Node
"""
#use bfs
if not node:
return None
m={}
q=[node]
clone=Node(node.val,[])
m[node]=clone
while q:
t=q[0]
del q[0]
for nei in t.neighbors:
if nei not in m:
n=Node(nei.val,[])
q.append(nei)
m[nei]=n
m[t].neighbors.append(m[nei])
return clone
138. Copy List with Random Pointer
Example 1:
Input:
{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","next":{"id”:“2”,“next”:null,“random”:{“KaTeX parse error: Expected 'EOF', got '}' at position 9: ref":"2"}̲,"val":2},"rand…ref”:“2”},“val”:1}
Explanation:
Node 1’s value is 1, both of its next and random pointer points to Node 2.
Node 2’s value is 2, its next pointer points to null and its random pointer points to itself.
主要处理的问题是随机指针问题,随机指针的拷贝不需要每次都遍历原来的链表。
当然,如果使用 HashMap 占用额外的空间,如果这道题限制了空间的话,就要考虑别的方法。下面这个方法很巧妙,可以分为以下三个步骤:
- 在原链表的每个节点后面拷贝出一个新的节点。
- 依次给新的节点的随机指针赋值,而且这个赋值非常容易 cur->next->random = cur->random->next。
- 断开链表可得到深度拷贝后的新链表。
举个例子来说吧,比如原链表是 1(2) -> 2(3) -> 3(1),括号中是其 random 指针指向的结点,那么这个解法是首先比遍历一遍原链表,在每个结点后拷贝一个同样的结点,但是拷贝结点的 random 指针仍为空,则原链表变为 1(2) -> 1(null) -> 2(3) -> 2(null) -> 3(1) -> 3(null)。然后第二次遍历,是将拷贝结点的 random 指针赋上正确的值,则原链表变为 1(2) -> 1(2) -> 2(3) -> 2(3) -> 3(1) -> 3(1),注意赋值语句为:
cur->next->random = cur->random->next;
这里的 cur 是原链表中结点,cur->next 则为拷贝链表的结点,cur->next->random 则为拷贝链表的 random 指针。cur->random 为原链表结点的 random 指针指向的结点,因为其指向的还是原链表的结点,所以我们要再加个 next,才能指向拷贝链表的结点。最后再遍历一次,就是要把原链表和拷贝链表断开即可,参见代码如下:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, next, random):
self.val = val
self.next = next
self.random = random
"""
class Solution(object):
def copyRandomList(self, head):
"""
:type head: Node
:rtype: Node
"""
if head==None:
return None
#insert a new node in the middle
cur=head
while cur:
node=Node(cur.val,None,None)
tmp=cur.next
cur.next=node
node.next=tmp
cur=cur.next.next
#random pointer hanle
cur=head
while cur:
if cur.random:
cur.next.random=cur.random.next
cur=cur.next.next
#seperate the target deep copy list
cur=head
res=cur.next
while cur:
node=cur.next
cur.next=cur.next.next
if node.next:
node.next=node.next.next
cur=cur.next
return res