Skip to content

Commit 2541643

Browse files
committed
递归专题代码上传
1 parent 5b1b6a0 commit 2541643

12 files changed

+476
-62
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# leetcode
2-
leetcode解题得的源码,解题思路在代码的注释里。
2+
leetcode解题的源码,解题思路在代码的注释里。
33
- 我的[leetcode主页](https://leetcode-cn.com/u/stray_camel/)
44

55
## python@leetcode官方专题
6-
> 记录刷官方专题的题目
6+
> 记录刷官方专题的题目
7+
- [递归专题](/python@leetcode官方递归专题.md)

main.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,35 @@
1414
# print(str(id_x.fixed[0])+'.反转字符串.py')
1515

1616

17-
def _search(name):
17+
def _search(name=''):
18+
name = name.split(' ')
19+
res = 0
1820
for _ in _listdir:
19-
if name in _:
21+
if isinstance(name, list):
22+
for n in name:
23+
if n in _:
24+
# logging.info(os.path.join(questions_floder, _))
25+
logging.info(questions_floder+'/'+_)
26+
res +=1
27+
if isinstance(name, str) and name in _:
2028
# logging.info(os.path.join(questions_floder, _))
2129
logging.info(questions_floder+'/'+_)
22-
break
23-
return _
30+
res +=1
31+
return res
2432

2533

2634
if __name__ == "__main__":
27-
# _search('杨辉三角')
28-
def test(k):
29-
tmp = k
30-
res = [0 for _ in range(5)]
31-
res2 = [str(_)+tmp for _ in res]
32-
33-
34-
# print(res, res2)
35-
test(01)
35+
names = sys.argv[1:]
36+
for n in names:
37+
logging.info('查找{name}问题...'.format(name=n))
38+
res = _search(n)
39+
if not res:logging.warn('没找到{name}问题'.format(name=n))
40+
print()
41+
# def test(k):
42+
# tmp = k
43+
# res = [0 for _ in range(5)]
44+
# res2 = [str(_)+tmp for _ in res]
45+
46+
47+
# # print(res, res2)
48+
# test(01)

python@leetcode官方专题.md renamed to python@leetcode官方递归专题.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
## 递归专题
44
[官方专题](https://leetcode-cn.com/leetbook/read/recursion/xk5vw2/)
5-
5+
> 专题描述
66
递归 是计算机科学中的一个重要概念。它是许多其他算法和数据结构的基础。然而,对于许多初学者来说,掌握它可能是一件非常棘手的事情。
77

88
在开始探索本 LeetBook 之前,我们强烈建议您预先完成 二叉树 和 队列 & 栈 这两本 LeetBook 。
@@ -15,18 +15,18 @@
1515
如何更好地运用递归?
1616
完成后,你会对采用递归思想解决问题和自己分析算法的复杂度更有信心。
1717

18+
## 递归专题涉及到的题目
1819
- [反转字符串](./questions/344.反转字符串.py)
1920
- [两两交换链表中的节点](./questions/24.两两交换链表中的节点.py)
2021
- [杨辉三角](./questions/118.杨辉三角.py)
21-
- [杨辉三角 II]
22-
- [反转链表]
23-
- [斐波那契数]
24-
- [爬楼梯]
25-
- [二叉树的最大深度]
26-
- [二叉树的最大深度]
27-
- [Pow(x, n)]
28-
- [Pow(x, n)]
29-
- [合并两个有序链表]
30-
- [第K个语法符号]
31-
- [不同的二叉搜索树 II]
32-
- [不同的二叉搜索树 II]
22+
- [杨辉三角 II](./questions/119.杨辉三角-ii.py)
23+
- [反转链表](./questions/206.反转链表.py)
24+
- [斐波那契数](./questions/509.斐波那契数.py)
25+
- [爬楼梯](./questions/70.爬楼梯.py)
26+
- [二叉树的最大深度](./questions/104.二叉树的最大深度.py)
27+
- [二叉树的最大宽度](./questions/662.二叉树最大宽度.py)
28+
- [Pow(x, n)](./questions/50.pow-x-n.py)
29+
- [合并两个有序链表](./questions/21.合并两个有序链表.py)
30+
- [第K个语法符号](./questions/779.第k个语法符号.py)
31+
- [不同的二叉搜索树](./questions/96.不同的二叉搜索树.py)
32+
- [不同的二叉搜索树 II](./questions/95.不同的二叉搜索树-ii.py)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# @lc app=leetcode.cn id=104 lang=python3
3+
#
4+
# [104] 二叉树的最大深度
5+
#
6+
# https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
7+
#
8+
# algorithms
9+
# Easy (74.98%)
10+
# Likes: 697
11+
# Dislikes: 0
12+
# Total Accepted: 274K
13+
# Total Submissions: 365.2K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,找出其最大深度。
17+
#
18+
# 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
19+
#
20+
# 说明: 叶子节点是指没有子节点的节点。
21+
#
22+
# 示例:
23+
# 给定二叉树 [3,9,20,null,null,15,7],
24+
#
25+
# ⁠ 3
26+
# ⁠ / \
27+
# ⁠ 9 20
28+
# ⁠ / \
29+
# ⁠ 15 7
30+
#
31+
# 返回它的最大深度 3 。
32+
#
33+
#
34+
35+
# @lc code=start
36+
# Definition for a binary tree node.
37+
# Definition for a binary tree node.
38+
# class TreeNode:
39+
# def __init__(self, x):
40+
# self.val = x
41+
# self.left = None
42+
# self.right = None
43+
44+
class Solution:
45+
def maxDepth(self, root: TreeNode) -> int:
46+
if not root:return 0
47+
# if root.left:
48+
# return 1+self.maxDepth(root.left)
49+
# if root.right:
50+
# return 1+maxDepth(root.right)
51+
return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))
52+
# @lc code=end
53+

questions/50.pow-x-n.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
class Solution:
99
def myPow(self, x: float, n: int) -> float:
1010
# 二分法 log(n)
11-
def quickMul(N):
11+
def _help(N):
1212
if N == 0:
1313
return 1.0
14-
y = quickMul(N // 2)
14+
y = _help(N // 2)
1515
return y * y if N % 2 == 0 else y * y * x
1616

17-
return quickMul(n) if n >= 0 else 1.0 / quickMul(-n)
17+
return _help(n) if n >= 0 else 1.0 / _help(-n)
1818

1919
# @lc code=end
2020

questions/509.斐波那契数.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,63 @@
33
#
44
# [509] 斐波那契数
55
#
6-
6+
from functools import lru_cache
77
# @lc code=start
8+
9+
810
class Solution:
9-
def fib(self, N: int) -> int:
11+
def __init__(self):
12+
super().__init__()
13+
self.constants = [[1, 1], [1, 0]]
14+
15+
@lru_cache(None)
16+
def reversion(self, N: int) -> int:
1017
if N < 2:
18+
return self.fub(N-1)+self.fib(N-2)
19+
20+
def matrix_pow(self, N: int) -> int:
21+
"""矩阵法
22+
"""
23+
def multiply(A: list, B: list):
24+
x = A[0][0] * B[0][0] + A[0][1] * B[1][0]
25+
y = A[0][0] * B[0][1] + A[0][1] * B[1][1]
26+
z = A[1][0] * B[0][0] + A[1][1] * B[1][0]
27+
w = A[1][0] * B[0][1] + A[1][1] * B[1][1]
28+
29+
A[0][0] = x
30+
A[0][1] = y
31+
A[1][0] = z
32+
A[1][1] = w
33+
34+
def matrix_power(A: list, N: int):
35+
if (N <= 1):
36+
return A
37+
matrix_power(A, N//2)
38+
multiply(A, A)
39+
B = self.constants
40+
if (N % 2 != 0):
41+
multiply(A, B)
42+
if (N <= 1):
1143
return N
12-
return self.fib(N-1)+self.fib(N-2)
13-
14-
# @lc code=end
44+
A = self.constants
45+
matrix_power(A, N-1)
46+
return A[0][0]
1547

48+
def the_Golden_Ratio(self, N: int) -> int:
49+
# 根据黄金分割比生成斐波那契
50+
golden_ratio = (1 + 5 ** 0.5) / 2
51+
return int((golden_ratio ** N + 1) / 5 ** 0.5)
52+
53+
@lru_cache(None)
54+
def fib(self, N: int) -> int:
55+
return {
56+
1: lambda N: self.matrix_pow(N),
57+
2: lambda N: self.reversion(N),
58+
3: lambda N: self.the_Golden_Ratio(N)
59+
}[3](N)
60+
61+
62+
# @lc code=end
63+
if __name__ == "__main__":
64+
test = Solution()
65+
print(test.fib(2))

questions/662.二叉树最大宽度.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@
1111
# self.val = x
1212
# self.left = None
1313
# self.right = None
14-
1514
class Solution:
16-
def maxDepth(self, root: TreeNode) -> int:
17-
if not root:return 0
18-
# if root.left:
19-
# return 1+self.maxDepth(root.left)
20-
# if root.right:
21-
# return 1+maxDepth(root.right)
22-
return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))
15+
def __init__(self):
16+
self.leftIds = []
17+
self.widths = []
18+
19+
def widthOfBinaryTree(self, root: TreeNode) -> int:
20+
return self.dfs(root, 0, 0)
21+
22+
def dfs(self, node, depth, id):
23+
# node: current node of the current layer
24+
# depth: depth of a tree, starting from 0
25+
# id: current id of the node
26+
# returns the max width of all layers
27+
if node == None:
28+
return 0
29+
if len(self.leftIds) == depth:
30+
self.leftIds.append(id)
31+
self.widths.append(1)
32+
else:
33+
self.widths[depth] = id - self.leftIds[depth] + 1
34+
35+
leftMaxWidth = self.dfs(node.left, depth + 1, id * 2)
36+
rightMaxWidth = self.dfs(node.right, depth + 1, id * 2 + 1)
37+
38+
return max(max(leftMaxWidth, rightMaxWidth), self.widths[depth])
2339
# @lc code=end
24-

questions/70.爬楼梯.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# @lc app=leetcode.cn id=70 lang=python3
3+
#
4+
# [70] 爬楼梯
5+
#
6+
# https://leetcode-cn.com/problems/climbing-stairs/description/
7+
#
8+
# algorithms
9+
# Easy (50.51%)
10+
# Likes: 1229
11+
# Dislikes: 0
12+
# Total Accepted: 284.4K
13+
# Total Submissions: 562.5K
14+
# Testcase Example: '2'
15+
#
16+
# 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
17+
#
18+
# 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
19+
#
20+
# 注意:给定 n 是一个正整数。
21+
#
22+
# 示例 1:
23+
#
24+
# 输入: 2
25+
# 输出: 2
26+
# 解释: 有两种方法可以爬到楼顶。
27+
# 1. 1 阶 + 1 阶
28+
# 2. 2 阶
29+
#
30+
# 示例 2:
31+
#
32+
# 输入: 3
33+
# 输出: 3
34+
# 解释: 有三种方法可以爬到楼顶。
35+
# 1. 1 阶 + 1 阶 + 1 阶
36+
# 2. 1 阶 + 2 阶
37+
# 3. 2 阶 + 1 阶
38+
#
39+
#
40+
#
41+
42+
# @lc code=start
43+
from functools import lru_cache
44+
class Solution:
45+
def __init__(self):
46+
self.res = []
47+
@lru_cache(None)
48+
def climbStairs(self, n: int) -> int:
49+
if n <4:
50+
return n
51+
return self.climbStairs(n-1)+self.climbStairs(n-2)
52+
# @lc code=end
53+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# @lc app=leetcode.cn id=724 lang=python3
3+
#
4+
# [724] 寻找数组的中心索引
5+
#
6+
# https://leetcode-cn.com/problems/find-pivot-index/description/
7+
#
8+
# algorithms
9+
# Easy (38.36%)
10+
# Likes: 219
11+
# Dislikes: 0
12+
# Total Accepted: 59K
13+
# Total Submissions: 153.4K
14+
# Testcase Example: '[1,7,3,6,5,6]'
15+
#
16+
# 给定一个整数类型的数组 nums,请编写一个能够返回数组 “中心索引” 的方法。
17+
#
18+
# 我们是这样定义数组 中心索引 的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。
19+
#
20+
# 如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。
21+
#
22+
#
23+
#
24+
# 示例 1:
25+
#
26+
# 输入:
27+
# nums = [1, 7, 3, 6, 5, 6]
28+
# 输出:3
29+
# 解释:
30+
# 索引 3 (nums[3] = 6) 的左侧数之和 (1 + 7 + 3 = 11),与右侧数之和 (5 + 6 = 11) 相等。
31+
# 同时, 3 也是第一个符合要求的中心索引。
32+
#
33+
#
34+
# 示例 2:
35+
#
36+
# 输入:
37+
# nums = [1, 2, 3]
38+
# 输出:-1
39+
# 解释:
40+
# 数组中不存在满足此条件的中心索引。
41+
#
42+
#
43+
#
44+
# 说明:
45+
#
46+
#
47+
# nums 的长度范围为 [0, 10000]。
48+
# 任何一个 nums[i] 将会是一个范围在 [-1000, 1000]的整数。
49+
#
50+
#
51+
#
52+
53+
# @lc code=start
54+
class Solution:
55+
def pivotIndex(self, nums: List[int]) -> int:
56+
# @lc code=end
57+

0 commit comments

Comments
 (0)