Skip to content

Commit b5c378c

Browse files
committed
regular leetcode
1 parent 0a91592 commit b5c378c

5 files changed

+89
-32
lines changed

049._group_anagrams_python.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
我又来使用我的取巧神奇python大法
1010

1111

12-
```
12+
13+
思路是: 首先我们把这个单词都变成排序后的str,然后把它做key放入字典中,所以一样的就会放到同样的key下面,最后再把字典的值全部聚集起来。
14+
15+
16+
17+
18+
```python
1319
class Solution(object):
1420
def groupAnagrams(self, strs):
1521
"""
@@ -29,7 +35,7 @@ class Solution(object):
2935
"""
3036
:type s: str
3137
:type t: str
32-
:rtype: bool
38+
:rtype: str
3339
"""
3440
sList = sorted(list(s))
3541
str1 = ''.join(sList)

062._Unique_Paths.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
### 62. Unique Paths
2+
3+
题目:
4+
5+
<https://leetcode.com/problems/unique-paths/description/>
6+
7+
难度:
8+
9+
Medium
10+
11+
12+
13+
思路:
14+
15+
16+
dp 典型, 走到(i, j) 可能的方法是走到 (i-1,j) 加上走到 (i, j-1)之和。
17+
18+
同时针对于第一行和第一列都只能是一种走法。
19+
20+
以上这个都是走向只可能是向右或者向下限制的。
21+
22+
23+
```python
24+
class Solution(object):
25+
def uniquePaths(self, m, n):
26+
"""
27+
:type m: int
28+
:type n: int
29+
:rtype: int
30+
"""
31+
dp = [[1 for _ in range(n)] for _ in range(m)]
32+
33+
for i in range(1,m):
34+
for j in range(1, n):
35+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
36+
37+
return dp[m-1][n-1]
38+
39+
```

229._majority_element_ii.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ majority element是两两比较扔掉不同的元素,然后最后会留下一
1818

1919
最后再加一个比较来确认这些函数是majority element
2020

21-
```
21+
```python
2222
class Solution(object):
2323
def majorityElement(self, nums):
2424
"""

235._lowest_common_ancestor_of_a_binary_search_tree.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
AC解法
1515

16-
```
16+
```python
1717
class Solution(object):
1818
def lowestCommonAncestor(self, root, p, q):
1919
"""
@@ -31,3 +31,8 @@ class Solution(object):
3131
else:
3232
return self.lowestCommonAncestor(root.right,p,q)
3333
```
34+
35+
36+
37+
同样236的代码也可以解此题:[236](236._lowest_common_ancestor_of_a_binary_tree.md)
38+

236._lowest_common_ancestor_of_a_binary_tree.md

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ Medium
2626

2727

2828

29-
```
30-
29+
```python
3130
class Solution(object):
3231
def lowestCommonAncestor(self, root, p, q):
3332
"""
@@ -36,30 +35,25 @@ class Solution(object):
3635
:type q: TreeNode
3736
:rtype: TreeNode
3837
"""
39-
pathP = self.pathTo(root,p)
40-
pathQ = self.pathTo(root,q)
38+
def path(root, goal):
39+
if root == None: return root
40+
stack = [(root, [root])]
41+
while stack:
42+
node, path = stack.pop()
43+
if node == goal:
44+
return path
45+
if node.left: stack.append((node.left, path + [node.left]))
46+
if node.right: stack.append((node.right, path + [node.right]))
47+
48+
pathP = path(root,p)
49+
pathQ = path(root,q)
4150
n = min(len(pathP), len(pathQ))
4251

4352
ans = root
4453
for i in range(n):
4554
if pathP[i] == pathQ[i]:
4655
ans = pathP[i]
47-
else:
48-
break
4956
return ans
50-
51-
52-
def pathTo(self, root, goal):
53-
# goal node ,path
54-
if root == None: return root
55-
stack = [(root, [root])]
56-
while stack:
57-
node, path = stack.pop()
58-
if node == goal:
59-
return path
60-
if node.left: stack.append((node.left, path + [node.left]))
61-
if node.right: stack.append((node.right, path + [node.right]))
62-
6357
```
6458

6559
递归解法,之所以我没有用递归因为有疑惑, BASE CASE 很容易想到,root 是none,或者p == root 或者q == root,那么LCA就是root,如果两个node一个在左边,一个在右边,那么LCA也是root,但是如果一个是6,另一个是4则有一点疑惑,但其实是没有问题的,因为这个时候给的总是他们的共同root,所以这个递归解法是没错的,总是想到递归是在那个状况下递归
@@ -79,7 +73,13 @@ AC代码
7973

8074

8175

82-
```
76+
前面的思路很容易理解,到了递归的部分:
77+
78+
按照 6 和 4 来看,我们进入left 5,递归进入 left 6, 另外会进入 right 2 最后进入4,但是在这个过程中,我们会看到对于node 5来说,她的left 和right 都不为none,那么return这个node 5.
79+
80+
81+
82+
```python
8383
class Solution(object):
8484
def lowestCommonAncestor(self, root, p, q):
8585
"""
@@ -88,17 +88,24 @@ class Solution(object):
8888
:type q: TreeNode
8989
:rtype: TreeNode
9090
"""
91-
if root == None:
92-
return None
93-
94-
if p == root or q == root:
91+
if root == None or root == p or root == q:
9592
return root
9693

97-
left = self.lowestCommonAncestor(self.left,p,q)
98-
right = self.lowestCommonAncestor(self.right,p,q)
94+
left = self.lowestCommonAncestor(root.left,p,q)
95+
right = self.lowestCommonAncestor(root.right,p,q)
9996

10097
if left and right:
10198
return root
10299

103-
return left if left is None else right
104-
```
100+
return left if left else right
101+
```
102+
103+
104+
可以看一下SO上的问题:[How to find the lowest common ancestor of two nodes in any binary tree?](https://stackoverflow.com/questions/1484473/how-to-find-the-lowest-common-ancestor-of-two-nodes-in-any-binary-tree)
105+
106+
和一些讨论:
107+
108+
109+
110+
@David: LCA query answering is pretty useful. LCA + Suffix tree = powerful string related algorithms. – Aryabhatta
111+

0 commit comments

Comments
 (0)