Skip to content

Commit 4c42afb

Browse files
committed
revise some wrong link
1 parent a1fab68 commit 4c42afb

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

Array/.K-SUM题解.md.swp

12 KB
Binary file not shown.

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@
136136
##### Search类题
137137
| 序号 | 题目 | 难度 | 代码 |
138138
| ------------ | ------------------------------------------------------------ | ------ | ----------------- |
139-
| 33 | [Search in Rotated Sorted Array ](https://leetcode.com/problems/wiggle-subsequence/) | Medium | 代码 |
140-
| 81 | [Search in Rotated Sorted Array II ](https://leetcode.com/problems/sort-colors) | Medium | |
141-
| 34 | [Find First and Last Position of Element in Sorted Array ](https://leetcode.com/problems/wiggle-sort/) | Medium | python、java、c++ |
142-
| 35 | [Search Insert Position ](https://leetcode.com/problems/next-permutation) | Easy | python、java、c++ |
143-
| 74 | [Search a 2D Matrix ](https://leetcode.com/problems/combination-sum) | Medium | python、java、c++ |
144-
| 79 | [Word Search ](https://leetcode.com/problems/combination-sum-ii) | Medium | python、java、c++ |
139+
| 33 | [Search in Rotated Sorted Array ](https://leetcode.com/problems/search-in-rotated-sorted-array/) | Medium | python、java、c++ |
140+
| 81 | [Search in Rotated Sorted Array II ](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | Medium | python、java、c++ |
141+
| 34 | [Find First and Last Position of Element in Sorted Array ](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | Medium | python、java、c++ |
142+
| 35 | [Search Insert Position ](https://leetcode.com/problems/search-insert-position/) | Easy | python、java、c++ |
143+
| 74 | [Search a 2D Matrix ](https://leetcode.com/problems/search-a-2d-matrix/) | Medium | python、java、c++ |
144+
| 79 | [Word Search ](https://leetcode.com/problems/word-search/) | Medium | python、java、c++ |
145145
| 153 | [Find Minimum in Rotated Sorted Array ](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array) | Medium | python、java、c++ |
146146
| 154 | [Find Minimum in Rotated Sorted Array II ](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii) | Hard | python、java、c++ |
147-
| 39 | [Combination Sum ](https://leetcode.com/problems/largest-rectangle-in-histogram) | Medium | python、java、c++ |
147+
| 39 | [Combination Sum ](https://leetcode.com/problems/combination-sum/) | Medium | python、java、c++ |
148148
| 40 | [Combination Sum II ](https://leetcode.com/problems/combination-sum-ii) | Medium | python、java、c++ |
149149
| 216 | [Combination Sum III ](https://leetcode.com/problems/combination-sum-iii) | Medium | python、java、c++ |
150150
| 45 | [Jump Game II ](https://leetcode.com/problems/jump-game-ii) | Hard | python、java、c++ |
@@ -175,8 +175,8 @@
175175
| ------------ | ------------------------------------------------------------ | ------ | ----------------- |
176176
| 274 | [H-Index](https://leetcode.com/problems/h-index/) | medium | python、java、c++ |
177177
| 376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence) | medium | python、java、c++ |
178-
| 277 | [Find the Celebrity ](https://leetcode.com/problems/game-of-life) | medium | python、java、c++ |
179-
| 370 | [Range Addition ](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed) | medium | python、java、c++ |
178+
| 277 | [Find the Celebrity ](https://leetcode.com/problems/find-the-celebrity/) | medium | python、java、c++ |
179+
| 370 | [Range Addition ](https://leetcode.com/problems/range-addition/) | medium | python、java、c++ |
180180
| 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point) | hard | python、java、c++ |
181181

182182
------

python/133_CloneGraph.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Time : 2019/3/2 11:12 AM
4+
# @Author : huxiaoman
5+
# @File : 133_CloneGraph.py
6+
# @Package : LeetCode
7+
# @E-mail : charlotte77_hu@sina.com
8+
9+
# Definition for a undirected graph node
10+
class UndirectedGraphNode:
11+
def __init__(self, x):
12+
self.label = x
13+
self.neighbors = []
14+
15+
16+
17+
class Solution:
18+
# BFS
19+
def cloneGraph1(self, node):
20+
if not node:
21+
return
22+
# node = UndirectedGraphNode()
23+
nodeCopy = UndirectedGraphNode(node.label)
24+
dic = {node: nodeCopy}
25+
queue = collections.deque([node])
26+
while queue:
27+
node = queue.popleft()
28+
for neighbor in node.neighbors:
29+
if neighbor not in dic: # neighbor is not visited
30+
neighborCopy = UndirectedGraphNode(neighbor.label)
31+
dic[neighbor] = neighborCopy
32+
dic[node].neighbors.append(neighborCopy)
33+
queue.append(neighbor)
34+
else:
35+
dic[node].neighbors.append(dic[neighbor])
36+
return nodeCopy
37+
38+
# DFS iteratively
39+
def cloneGraph2(self, node):
40+
if not node:
41+
return
42+
nodeCopy = UndirectedGraphNode(node.label)
43+
dic = {node: nodeCopy}
44+
stack = [node]
45+
while stack:
46+
node = stack.pop()
47+
for neighbor in node.neighbors:
48+
if neighbor not in dic:
49+
neighborCopy = UndirectedGraphNode(neighbor.label)
50+
dic[neighbor] = neighborCopy
51+
dic[node].neighbors.append(neighborCopy)
52+
stack.append(neighbor)
53+
else:
54+
dic[node].neighbors.append(dic[neighbor])
55+
return nodeCopy
56+
57+
# DFS recursively
58+
def cloneGraph(self, node):
59+
if not node:
60+
return
61+
nodeCopy = UndirectedGraphNode(node.label)
62+
dic = {node: nodeCopy}
63+
self.dfs(node, dic)
64+
return nodeCopy
65+
66+
def dfs(self, node, dic):
67+
for neighbor in node.neighbors:
68+
if neighbor not in dic:
69+
neighborCopy = UndirectedGraphNode(neighbor.label)
70+
dic[neighbor] = neighborCopy
71+
dic[node].neighbors.append(neighborCopy)
72+
self.dfs(neighbor, dic)
73+
else:
74+
dic[node].neighbors.append(dic[neighbor])
75+
76+
if __name__ == '__main__':
77+
s = Solution()
78+
print s.cloneGraph1({"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1})

0 commit comments

Comments
 (0)